Islamist militants carried out a deadly assault on the main UN compound in the Somali capital on Wednesday, dealing a blow to fragile security gains that have allowed a slow return of foreign aid workers and diplomats.

The assault, claimed by Islamist group al Shabaab, began
before midday when a car bomb exploded outside the United Nations Development Programme (UNDP) base. Rebel gunmen forced their way into the compound and fought with security guards.

The African Union (AU) peacekeeping force, which sent soldiers and armoured vehicles to the compound, which includes several buildings, said it was under the control of friendly troops after a gun fight that lasted more than 90 minutes.

Interior minister Abdikarim Hussein Guled said four foreign UN security staff and four local guards were killed in the gun battle that left seven insurgent fighters dead.

An ambulance service official said his crew carried away seven dead civilians, bringing the total dead to 22.

It was the first significant attack on UN premises by al Shabaab since they were driven out of Mogadishu in fighting with AU and Somali government forces about two years ago.

More than a million Somalis live in crisis conditions, according to the United Nations, which has started building up offices and international staff after security improved.

UN Secretary-General Ban Ki-moon said he was "outraged by the despicable attack" in a telephone call to Somali President Hassan Sheikh Mohamud. Al Shabaab accused the United Nations of serving US interests.

"The UN, a merchant of death and a satanic force of evil, has a long, inglorious record of spreading nothing but poverty, dependency and disbelief," al Shabaab said on its Twitter feed @HSMPRESS1

The South African state weapons firm Denel said two of its staff were killed in the raid. Militants have launched grenade strikes and similar low-level attacks on UN bases in the past, but no assault of this scale.

'Here to stay'One UN official said some Western nations that had been keen to support the Western-leaning government elected last year had played down dangers posed by al Shabaab and its ability to infiltrate the security forces and attack the capital.

"This is part of the consequence of over-optimism in some Western nations that has overshadowed the need to look at deeper problems before rolling out any kind of UN mission," said the official, who follows Somalia closely but is not authorised to talk to the media.

He said the government had not done enough to overhaul its security forces.

The top UN official in Somalia, Nicholas Kay, told Reuters there were lessons to be learned but that the United Nations would not be deterred from its mission.

Asked if UN staff would be evacuated from Mogadishu, Kay said: "No. The UN is here to help and we are here to stay."

The initial bomb blast sprayed masonry and twisted metal across the road that links the nearby airport, which serves as the main base for the AU peacekeepers, and the city centre. The Somali government condemned the attack and offered "deepest sympathy to all victims".

"Today all Somalia stands shoulder to shoulder with UNSOM," Prime Minister Abdi Farah Shirdon Saaid said on Twitter, referring to the new UN Somalia assistance programme UNSOM. Bystanders reported several smaller blasts inside the compound during the gunfight.

The raid was a copycat of a strike on Mogadishu's law courts in April, when gunmen detonated suicide vests during a gunbattle with security forces. Interior Minister Guled confirmed some of the assailants blew themselves up on Wednesday.

AU forces and government troops drove al Shabaab rebels out of the coastal capital in 2011, but militants have kept up guerrilla-style attacks from rural bases.

The overthrow of a dictator in 1991 plunged Somalia into two decades of violent turmoil, first at the hands of clan warlords and then Islamist militants, who have steadily lost ground since 2011 under pressure from the AU military offensive.