Al Qaeda-linked group al-Shabab attacked the heavily-fortified Somali presidential palace compound, blasting through a gate with a car bomb and engaging in a fierce gun battle with African Union soldiers, police said.

Shabab said it carried out the Friday attack on the heavily fortified compound in Mogadishu, known as Villa Somalia, but the Somali president, Hassan Sheikh Mohamud, was unharmed.

"President just called me to say he's unharmed. Attack on Villa Somalia had failed. Sadly some lives lost," the UN Special Representative Nick Kay wrote on his Twitter feed.

It was not immediately clear how many people were killed.

Mogadishu has been hit by a series of suicide bomb in the past few weeks, attacks claimed by Shabab, which was pushed out of the city in mid-2011 but has continued to wage a sustained armed campaign.

Friday's battle 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, Abdikadir Ahmed, a senior police officer, told the Reuters news agency.

"The Shabab fighters who attacked the palace were about 10 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 Shabab fighters died; some blew themselves up while others were shot dead. Several government guards also died. Now the fighting is over."

A military spokesman for Shebab told the AFP news agency that the group was behind the attack.

"Our commandos have attacked the so-called presidential palace in order to kill or arrest those who who are inside. The enemy had suffered a great deal of harm," Sheikh Abdul Aziz Abu Musab said.