Somali Islamist fighters have attacked a hotel in a strategic central town the militants lost control of last week.
A car bomb exploded by the hotel in Bulo-burde where African Union (AU) and Somali officers were staying and gunfire continued for another five hours, witnesses said.
Six soldiers were killed, including a top Somali army commander, the AU said.
The al-Shabab Islamist group said it was behind the attack and that 30 AU and army officers had been killed.
A spokesman for the 22,000-strong AU force in Somalia (Amisom), Col Ali Adan Humad, said all the al-Shabab fighters involved in the raid had been killed, without specifying numbers.
Al-Shabab, which is linked to al-Qaeda, has waged an eight-year insurgency to overthrow the weak UN-backed government and create an Islamic state in Somalia.
The BBC’s Ibrahim Ade in the capital, Mogadishu, says al-Shabab had occupied Bulo-burde for more than five years.
The town, which has a strategic bridge over the River Shabelle and is at a crossroads linking various regions of the country, was an important base for al-Shabab.
Bulo-burde was captured as part of an ongoing AU and government offensive against al-Shabab, which controls much of south and central Somalia.
Communications to the central Hiran region went down not long after the fighting and gunfire in Bulo-burde ended, our reporter says.
Witnesses told the BBC the car blast at the hotel happened at 02:00 local time (23:00 GMT) and the fighting went on until about 07:00.
Col Humad said three Djiboutian soldiers and three Somali soldiers, including Col Mohamud Amin – the military commander of Hiran who was leading the offensive in the region – were killed.
Ten soldiers were also wounded, he said.
Earlier, Col Humad denied there were casualties when an Amisom convoy was attacked near its base in Arbiska outside Mogadishu.
Al-Shabab had released a statement saying that seven Burundian peacekeepers and five American nationals travelling in the vehicles were killed on Monday afternoon.
The al-Qaeda-linked group lost control of Mogadishu in 2011, but has intensified bombings and mortar raids in the city in recent weeks.
Last month, al-Shabab fighters stormed Villa Somalia, the seat of government in Mogadishu, killing at least 11 people.