Smoke billowed from inside the heavily-fortified presidential compound, and loud explosions and gunfire could be heard as troops tried to repel the intruders yesterday.
After more than two hours of fighting, Somalia's presidency said in a Twitter update that "the shameful attack" had been foiled by Somalia's armed forces fighting alongside African Union peacekeepers.
Al-Shabab claimed responsibility for the attack.
The presidency said both President Mohamud and Prime Minister Abdiweli Sheikh Ahmed were safe.
More From This Section
The presidential compound, which also houses several government offices, has been the subject of many attacks by al-Qaida-linked militants over the years. It is considered one of the safest places in Mogadishu because of the tight security presence.
The presidency said that the compound has since been secured, but gave no details about any casualties or what happened to the militants. Hussein, the police official, said he counted at least nine bodies.
The attack was the second assault on the presidential palace this year, and it marked the first time militants have been able to breach the compound and take offensive positions inside. The palace is protected by government troops and African Union peacekeepers who helped to drive the al-Shabab militants out of their bases in Mogadishu in 2011.
Police said the militants, who were armed with grenades, split up into groups once inside and tried to take control of different buildings in the compound.
Al-Shabab militants have previously carried out deadly attacks against lawmakers, UN staff and government officials.