Police and witnesses said the car bomb caused a huge explosion which allowed the gunmen to force their way shooting into the fortified building.
“Six people have been killed, most of them civilians, but the ministry building is secure now and government forces are in full control,” said police official Ahmed Weli Malim, after a gun battle lasting around an hour.
“A car loaded with explosives rammed the gate,” police official Mohamed Dahir said.
Witness Idil Malim reported seeing several people wounded or killed.
“There were several people lying dead at the scene,” he said.
Shabaab spokesman Abdulaziz Abu Musab claimed responsibility, boasting that their gunmen had been in “full in control” of the ministry, as well as entering a neighbouring building housing the oil ministry.
As government forces retook the compound and fighting ended, it was not immediately clear what happened to the gunmen, and whether they had been killed in the assault or had managed to escape.
The building is in the K5 district of the capital, which has been hit by a string of similar attacks in recent months.
The Shabaab rebels stage regular attacks in the capital as part of their fight against the country’s internationally-backed government and African Union forces supporting it.