Gunmen attacked the African Union's main base in the capital, Mogadishu, on Thursday leading to an exchange of gunfire between militants and soldiers that left at least five militants dead, an official with the mission in Somalia said.

Somali extremist group al-Shabab claimed responsibility for the attack and said they were targeting a Christmas party at the base, which also houses embassy and U.N. offices.

At least eight men entered the AU base near the Mogadishu airport, AU Mission in Somalia spokesman Col. Ali Aden Houmed told The Associated Press. He said the base was now calm.

"Our forces shot dead three of them, two detonated themselves near a fuel depot, and the three are believed to have escaped," he said.

Some African Union soldiers may have died in the fighting, Houmed said, but did not give details.

"An investigation is underway on how they entered the base," he said, adding that the attack was launched at a time when people were busy at lunch.

AU troops are bolstering Somalia's weak government against an insurgency from the Al Qaeda-linked al-Shabab.

Al-Shabab controlled much of Mogadishu during the years 2007 to 2011, but was pushed out of Somalia's capital city by African Union forces. The militants then also lost control of the port city of Kismayo, crippling one of its major income generators. Despite those setbacks, the group continues to carry out devastating suicide and gunfire attacks.

Gunfire and explosions could be heard earlier Thursday after militants likely entered the base, said Mohamed Abdi, a policeman at the capital's airport, which shares a fence with the AU base.

Ali Abdullahi, who lives near the airport, said AU troops took up positions inside the base. All roads leading to the airport were sealed following the shootout, he said.

The United Nations representative to Somalia, Nicholas Kay, condemned the attack on Twitter.

Somalia has been trying to rebuild following years of political instability and civil strife since 1991, when the dictator Siad Barre was ousted from power.