At least 11 people were killed when Islamist militants attacked the Somali presidential compound on Friday, blasting through a gate with a car bomb and fighting a gunbattle with guards.
Somali President Hassan Sheikh Mohamud was unharmed in the assault on the heavily-fortified compound known as Villa Somalia, which also houses other government agencies. Al Shabaab, a group linked to al Qaeda, claimed responsibility.
The president said the incident would not stop his government's work to rebuild Somalia after decades of war.
"An act of terrorism, however unspeakable, does not hide the truth that this is a marginal group on the brink of extinction," Mohamud said in a statement.
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, near the presidential palace.
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."
The death toll could not be immediately verified.
Somalia's Security Minister, Abdikarim Hussein Guled, said two government officials and nine gunmen were killed.
"Nine fighters tried to attack the gate of the palace. Two died in the car bombs and the seven others were shot dead, as you can see from their bodies," Guled told reporters at the palace.
"Stray bullets killed a director at the PM's office and a national security officer. They had two car bombs," he added.
Hussein Farah, a senior police officer, had earlier told Reuters from the scene that several government guards had been killed in a prolonged shootout with the gunmen.
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 attackers wore uniforms similar to those of the presidential guards, and some wore suicide vests.
"The al Shabaab fighters who attacked the palace were about 10 men disguised in military uniform and the red hats (worn by the palace guards)," Farah 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 forces from other African nations sent by the African Union drove them out of Mogadishu and then expelled them from most urban centres.
The United Nations agreed in November to expand the AMISOM force of peacekeepers by some 4,500 troops to a total of about 22,000. The force is expected to launch a broad offensive against al Shabaab shortly, with Somali troops fighting alongside it.
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 Andrew Roche)