The African Union (AU) force in Somalia says three of its peacekeepers and a civilian contractor have been killed in an attack on its headquarters in the capital, Mogadishu.
An AU statement said al-Shabab gunmen had entered the base disguised as Somali government troops.
It said five attackers were killed and several others captured.
The African Union has over 20,000 troops in the country to help the weak government battle al-Shabab.
The peacekeeping force, known as Amisom, said its troops had regained control of the base in Mogadishu after a gunfight that lasted several hours.
"Three Amisom soldiers and a civilian contractor unfortunately lost their lives," the AU said in a statement, having earlier said that three peacekeepers had been injured.
Earlier, al-Shabab spokesman Sheikh Abdiasis Abu Musab said its fighters had attacked the "headquarters of the foreign troops in Somalia".
After several hours of explosions and gunfire, Amisom tweeted that the base was "safe and under control".
"The Amisom soldiers are commended for their gallant effort in controlling the situation," it added.
Amisom spokesman Col Ali Houmed told AP news agency that at least eight militants had entered the base but one Western security source told AFP news agency there could have been as many as 20.
The AU base is on the edge of the compound of Mogadishu's international airport.
The base also hosts the British and Italian embassies and is used as the headquarters for United Nations operations in Somalia.
Earlier in December, at least six people were killed when al-Shabab attacked a UN convoy near the airport.
Various armed groups have been battling for control of Somalia since the overthrow of President Siad Barre in 1991.