Eight Islamist gunmen infiltrated the main African Union (AU) base in Mogadishu on Thursday and killed three peacekeepers and a civilian contractor, the AU mission in Somalia said.
Somali militant group al Shabaab claimed responsibility for the attack, which lasted several hours, and said it had killed 14 peacekeepers. Witnesses reported hearing bomb blasts and volleys of gunfire through the day.
"We targeted the enemies at a time they were celebrating Christmas," Sheikh Abdiasis Abu Musab, al Shabaab's military operations spokesman, told Reuters.
In the past, al Shabaab has exaggerated the number of soldiers it has killed, while officials have played down losses.
The al Qaeda-aligned militants want to topple the Western-backed Mogadishu government and describe AU troops as "Christian enemies". The Islamist group also wants to impose its own strict version of sharia law in the country.
The raid showed al Shabaab's ability to carry out high-profile attacks in the capital even as it is losing territory in rural areas to AU peacekeepers who have launched two major offensives this year.
"The terrorists, some of whom were disguised in Somali National Army uniforms, breached the base camp around lunch hour and attempted to gain access to critical infrastructure, during which five of them were killed and three others captured," the AU peacekeeping mission in Somalia, AMISOM, said in a statement.
It did not disclose the nationalities of the peacekeepers and civilian contractor killed in the attack.
The AU's Halane base is on the edge of the Mogadishu international airport compound, which houses the base for UN operations in Somalia as well as the British and Italian embassies and has a tight security cordon and blast walls.
Western diplomats who were celebrating Christmas in the sweltering Mogadishu heat were evacuated to safety bunkers until the raid was over.
Aleem Siddique, spokesman for the United Nations in Somalia, said all UN staff were safe and accounted for.
A spokeswoman for the US State Department, Marie Harf, called the attack "a cowardly terrorist act", and said US support for the people of Somalia, the African Union Mission in Somalia, and Somali government forces in their efforts to defeat al-Shabaab would not waver.
Al Shabaab was pushed out of Mogadishu in 2011 but it still controls chunks of the countryside in south and central Somalia. This year it has lost several key towns, including the port city of Barawe, during the two AMISOM offensives.