Al Qaeda-linked militants al Shabaab attacked the Somali presidential palace compound on Friday, blasting through a gate with a car bomb and engaging in a fierce gun battle with African peacekeepers, police said.
Somali president Hassan Sheikh Mohamud was not harmed in the assault on the heavily fortified compound, known as Villa Somalia.
"President just called me to say he's unharmed. Attack on Villa Somalia had failed. Sadly some lives lost," U.N. Special Representative Nick Kay wrote on his official Twitter feed.
There were conflicting reports about what happened.
Abdikadir Ahmed, a senior police officer, told Reuters the fighting took place at the house of Somalia's top military commander, General Dahir Aden Indha Qarshe, located in the same compound and near the presidential palace building.
A government official, who declined to be named, said the militants reached a mosque in the centre of the compound, where the president usually prays on Friday. The chief of staff of the office of the prime minister and a former chief of intelligence were killed, along with six militants, he said.
He said the president was not at the mosque at the time of the attack although he had been planning to go.
"That is where the president normally goes to pray," he said. "So they were aiming at the president I guess."
A death toll could not be immediately confirmed. Ambulance sources said they were denied access to the scene and that the casualties were carried in ambulances belonging to African peacekeepers.
Islamist militant group al Shabaab claimed responsibility for the attack.
In the past few weeks, the Somali capital of Mogadishu has been hit by a series of suicide bomb attacks claimed by al Shabaab, who were pushed out of the city in mid-2011 but have continued to wage a sustained guerrilla campaign.
The strike was another reminder of the threat still posed by the rebels and how Somalia's fragile government is struggling to impose order more than two decades after the fall of dictator Mohamed Siad Barre tipped the country into chaos.
Western nations involved in Somalia worry it could sink back into chaos and provide a launchpad for Islamist militancy.
Police said the gunmen in the attack wore uniforms that were similar to those of the presidential guards.
"The al Shabaab fighters who attacked the palace were about ten men in military uniform and the red hats (worn by the palace guards)," Hussein Farah, a senior police officer at the scene, told Reuters.
"They had three cars. One was a car bomb and the other cars were carrying well-armed fighters," he said.
"All the Shabaab fighters perished, some blew up themselves while others were shot dead. Several government guards also died. Now the fighting is over, and scattered on the scene is human flesh and blood."
At least seven Somalis were killed when a remote-controlled bomb aimed at a U.N. convoy tore through cars and tea shops just outside the capital's international airport last week.
Al Shabaab ruled most of the southern region of Somalia from 2006 until 2011 when African forces drove them out of Mogadishu and then expelled them from most urban centres.
But the Islamists, who want to impose a very strict version of Islamic sharia law, still hold swathes of rural territory in southern Somalia and some smaller towns or villages, including the major coastal stronghold of Barawe.
(Additional reporting by Feisal Omar in Mogadishu and Edmund Blair and Richard Lough in Nairobi; Writing by James Macharia; Editing by Sonya Hepinstall)