More than 15 people were killed, Somali government officials said, including nine Islamist fighters, three UN guards and at least three civilians.
It was the most significant attack on the international humanitarian effort that has been trying to help rebuild Somalia, since the twin 2008 bombings on UN offices in Hargeisa and Bossaso in the country’s north.
Abdi Farah Shirdon, Somalia’s Prime Minister, said the assault was “senseless and despicable.”
"The UN are our friends and partners, and the UN agencies offer us humanitarian help and support,” Mr. Shirdon said in a statement.
The attack was carried out by Al Shabab, an army led by radicalized clerics and fighters trained in Pakistan and dedicated to overthrowing Somalia's government and instituting a strict interpretation of Islamic Sharia law.
The group officially allied with Al Qaeda in 2010.
Al Shabab was thought to have been largely defeated in Mogadishu, a city it occupied and ruled for four years until most of its fighters were driven out in 2011 by African Union peacekeeping forces.
Wednesday's attack highlights that it still poses a grave danger.
Al Shabab struck in late morning in a group of 15 fighters, first detonating a truck bomb at the gates of the UN’s “common compound,” a fortified complex of offices and stores close to the Somali capital’s international airport.
With the perimeter breached, the attackers fought their way into the heart of the UN base. They were only repelled 90 minutes later by Somali government soldiers and African Union peacekeepers.
At least two more explosions – from either grenades or other suicide bombers – were heard during the fighting.
“Dozens” of expatriate and Somali UN staff and contractors fled into a secure area of the compound, Ben Parker, the UN mission’s spokesman in Mogadishu, told the Monitor in a phone call.
“I was at a meeting some distance away and heard the blasts, then the gunfire, and it was immediately clear this was something out of the ordinary,” he says. “We had dozens of people in there. They were first in a secure site, and then were moved to the airport area, where they will remain as we review the situation.
“It’s deeply regretted when anyone loses their lives, but this attack is unlikely to change our attitude [in Somalia]," Mr. Parker added. "We knew it was dangerous here, and there were mitigation measures in place that functioned as they should to a very large degree.”
Al Shabab said it carried out the attacks that left two of its suicide bombers dead at the gates of the UN compound, two more dead from self-detonations inside, and five others shot and killed.
“Mujahideen units from the Martyrdom Brigade have stormed the [UN Development Programme] compound near the airport in Mogadishu,” Al Shabab said on its official Twitter feed.
“The Mujahideen attacked…about an hour ago and are now in complete control of the entire compound and the battle is still ongoing.”
The last major Al Shabab attack in the Somali capital was in April when it sent a nine-man suicide unit to blast its way into Mogadishu's main court complex, leaving 34 people dead.
The tactics were similar to those seen during Wednesday’s UN base raid. First a major bomb was detonated at outside gates, then fighters penetrate during the confusion and carry out more suicide bombings inside.
The 17,000-strong African Union mission in Somalia, fighting alongside Somali government troops, has forced Al Shabab from a series of key towns in the country’s south.
Despite being fractured by infighting and hunted by US drones, the extremists remain a potent threat, now focused more on launching car bombs and carrying out assassinations than holding territory.
They are however still powerful in rural areas.