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.