At least 13 Somali troops have died after the army attacked terrorist group al-Shabab in the jungle and farms near the district of Afgoye, northwest of the capital Mogadishu, a military official said.
Al-Shabab has battled since 2008 to overthrow Somalia's internationally recognized central government and establish its rule.
"We left Afgoye district and attacked al-Shabab in the farms outside Afgoye," Maj. Mohamed Ali told Reuters, describing Wednesday's incident in the district about 30 kilometers (19 miles) from Mogadishu.
"We killed four militants and chased al-Shabab," Ali said late Wednesday. "Most of the military came back and we left two dozen soldiers there. Then, this evening, al-Shabab attacked our few soldiers and killed 13."
Responsibility for the incident was claimed by al-Shabab, which launches regular attacks in a bid to undermine Somalia's central government, which is backed by the United Nations and African Union peacekeeping troops.
"We killed 24 soldiers and the rest escaped and ran away," said Abdiasis Abu Musab, the group's military operations spokesman.
Somalia's government and al-Shabab often give differing figures of casualties after attacks.
Deadly violence has engulfed Somalia since 1991, when clan warlords deposed leader Siad Barre and then turned on each other.