NAIROBI, Kenya - Gunmen attacked the African Union's main base in Somalia's 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.

Somalian extremist group al-Shabab claimed responsibility for the attack and said it was targeting a Christmas party at the base, which also houses Western embassies and U.N. offices.

At least eight men entered the AU base near the Mogadishu airport, said Col. Ali Aden Houmed, spokesman for the AU Mission in Somalia, adding that the base was later 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 he did not give details.

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

This is not the first time al-Shabab has attacked the African Union. In 2011, the group attacked an African Union base in Mogadishu, sparking a two-hour gunfight that left at least 10 people dead. Earlier this month, a suicide bomber rammed his vehicle into a U.N. convoy near Mogadishu's airport, killing three people just after Somalia's president had entered the protected airport area. No U.N. staff were killed or injured, the U.N. said.

The attacks underscore the militants' ability to carry out complex and deadly operations frequently even after AU troops have forced them from strongholds. Al-Shabab controlled much of Mogadishu during 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.

Gunfire and explosions could be heard earlier Thursday after militants likely entered the base.

Ali Abdullahi, who lives near the airport, said AU troops took up positions inside. All roads leading to the airport were sealed after the shootout, and journalists were also kept away.

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

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

The United States and the U.N., among others, have warned that political infighting in Somalia is putting at risk the security gains made in the country.