The fighting broke out when the al-Shabaab fighters attacked the African Union Mission in Somalia (AMISOM) troops early on Sunday in the Bulamareer district of the Lower Shabelle region, about 140km southwest of the capital, Mogadishu.
We have carried 23 dead AMISOM soldiers and a dead Somali soldier from the scene where al Shabaab ambushed AMISOM today
"We have carried 23 dead AMISOM soldiers and a dead Somali soldier from the scene where al-Shabaab ambushed AMISOM today," Ali Nur, the deputy governor of Lower Shabelle region, told Reuters news agency.
The armed group, which has been fighting the Western-backed government in Somalia, claimed they had killed 39 African Union troops.
The claim by the al-Qaeda-linked fighters, made by the group's spokesman on an affiliated radio station, could not be immediately verified.
Earlier on Sunday, Colonel Hassan Mohamed confirmed that an AMISOM convoy came under attack.
Local residents confirmed to AFP news agency that the fighting had taken place in the Lower Shebelle region, a hotly contested area where al-Shabab's spokesman said they had staged their ambush.
"The mujahedeen fighters stood over the dead bodies of 39 soldiers, among them senior commanders", Abdiaziz Abu Muzab told Andalus radio.
The African Union has a 22,000 strong force in the country dedicated to fighting al-Shabab and supporting the internationally backed government in Mogadishu.
Residents said the troops were ambushed in the village of Golweyn as they escorted supplies along the road that connects Mogadishu to Lower Shebelle.
"Fighting broke out and continued for more than one hour", said Ali Osman, a witness to the battle.
In April, a minibus travelling through Golweyn hit a landmine, killing at least 14 people.
That attack was also blamed on al-Shabab, which has fought successive governments in Mogadishu and also carried out attacks in Kenya and Uganda.
Source: News agencies