MOGADISHU, Somalia -- Seven al Qaeda-linked gunmen detonated a pick-up truck rigged with explosives at the gate of the UN compound in Somalia's capital Wednesday, launching a bombs-and-gunfire assault that saw militants pour into the complex, killing at least nine people, including three foreigners, officials said.

The seven al-Shabaab militants were from what the militia called its martyrdom, or suicide, brigade. They all died in the assault, an official said, bringing the overall death toll to at least 16.

The attack comes only six months after the United Nations expanded its presence in Mogadishu, where it had kept only a small operation because Islamic insurgents had controlled much of the capital until being pushed out in an offensive in 2011.

Al-Shabaab said on its Twitter feed shortly after the 11:30 a.m. attack began that its fighters "are now in control of the entire compound and the battle is still ongoing."

African Union and Somali security forces responded and took control of the compound by 12:30 p.m. The UN staff who sought refuge in the compound's secure bunker then were evacuated to the secure military base and airport complex across the street, said Ben Parker, a spokesman for the United Nations Assistance Mission in Somalia.

Two South Africans from the company Denel Mechem who were doing demining work for the UN died in the attack, said Vuyelwa Qinga, a spokeswoman for Denel, a manufacturer of defence equipment.

A UN official who insisted on anonymity because he was not an official spokesman said he believed two UN personnel from Kenya and Somalia were also killed.

"There was not very much time to get into the safe area," said Parker.

The top UN official on Somalia, Nicholas Kay, also works out of the building but was not inside the compound when it was attacked. Kay said he was shocked and horrified by the attack.

"The United Nations Common Compound houses UN personnel working on humanitarian and development issues for the Somali people. This was an act of blatant terrorism and a desperate attempt to knock Somalia off its path of recovery and peace building," said Kay.

A UN statement said it was verifying its casualty numbers. "There are certainly some injured and most likely worse," it said.

Speaking to the UN Security Council on Wednesday, Deputy Secretary-General Jan Eliasson called the attack outrageous and said the UN remains committed to achieving peace and keeping Somalia on its path to recovery.

At 11:30 a.m. the compound was rocked by the car bomb blast that blew down the compound's front gate. At least two other blasts followed, Parker said.

Dozens of staff from UN humanitarian and development agencies were in the compound and many were moved to the secure bunker, he said.

Mohamed Ali, an ambulance driver, said he transported five dead civilian bodies and 10 people who were wounded.

An Associated Press reporter who went inside the UN compound after the battle saw two dead bodies of what appeared to be al-Shabaab attackers wearing Somali military uniforms. An official said seven attackers died in total.

The compound's interior walls were scarred with bullet marks.

Somali Prime Minister Abdi Farah Shirdon said he is appalled that "our friends and partners" at the U.N. who are carrying out humanitarian activities would be the victims "of such barbaric violence." An African Union official, Mahamet Saleh Annadif, condemned the "cowardly" attack and sent condolences "to those who had lost loved ones."

The UN has had only a small presence in Mogadishu in recent years. In December, though, UN Secretary-General Ban Ki-moon touched down in Mogadishu wearing a bullet proof jacket to announce a return of the UN's political office to the seaside capital.

The attacked compound just across the street from the secure airport complex, where UN-backed African Union military forces are based. The UN compound is used by agencies like UNICEF, WHO and UNDP.

Mogadishu fell into anarchy in 1991 and is just beginning to move past years of sustained conflict. The UN and foreign embassies were absent from Mogadishu for close to two decades.

African Union forces pushed al-Shabaab out of Mogadishu in August 2011, meaning residents didn't have to live through daily battles for the first time in years. An international presence slowly began to return and the UN began the process of moving its personnel from the nearby capital of Nairobi, Kenya, back to Mogadishu, a process that has accelerated in recent weeks.

International embassies -- from Turkey and Britain, for example -- followed. Wednesday's attack underscores the fragile security situation and will force the UN and embassies to review their safety plans and decide if they have enough defences to withstand a sustained al-Shabaab assault.

Fadumo Hussein, a shopkeeper who was sitting inside her shop near the scene of the attack, described a narrow escape.

"It started with an earsplitting explosion, followed by heavy gunfire," she said, showing holes made by bullets on her shop. "I crouched and then crawled like an animal. I am very lucky. It was a shocking moment."