Journalists say al-Shabab fighters stormed the ministry of the interior and national security after detonating two car bombs.
The BBC’s Somali service says there have been 10 deaths and at least 20 civilians are reported injured.
Security had been tightened in the city over Ramadan to prevent attacks.
The Islamist militant group, which is fighting to overthrow the Western-backed government, usually intensifies its attacks during the holy month, which began in May this year.
As a result, authorities had put in more roadblocks and checkpoints.
But this latest assault, which was immediately claimed by al-Shabab, has proved that it is still capable of large scale attacks, as well as the targeted assassinations it had been carrying out more recently.
One of the car bombs was detonated outside the interior ministry, which is also used by the police, and the other near the presidential palace at about 11:00 local time (08:00 GMT).
Abdiasis Abu Musab, al-Shabab’s military operations spokesman, earlier told Reuters news agency that the group had fighters “still inside the building”, while witnesses reported heavy gunfire being exchanged in the area.
Police have since told broadcaster al-Jazeera that all the militants had been killed in a two-hour gun battle.
Mogadishu is regularly targeted by al-Shabab, which was forced from the capital in 2011, but still has a strong presence in regions around the capital.
It was blamed for two attacks last October, which left more than 500 people dead.