Since 2006, the rebels have been trying to topple Somalia’s central government and establish sovereignty based on its strict interpretation of Islamic law.
Friday’s attack happened in Masagawa, about 300km (190 miles) north of Mogadishu.
“I have seen 17 dead people including the attackers and the attacked. The fighting has moved into the forest. The town is calm now and under government control,” Hussein Nur, a resident of Masagawa, told Reuters by phone.
Captain Abdullahi Mohamed, a military officer in Masagawa, confirmed the attack and said 12 al-Shabab fighters had died, but he did not know how many troops were dead.
“Fierce fighting went on for hours on the edge of the town. So far, I know we lost soldiers but I have no exact figure. We repulsed al-Shabab and now we are pursuing them in the forest,” Mohamed told Reuters.
The Somalia National Army said on Twitter it had prevented an attack on the town itself, and killed al-Shabab fighters, but did not say how many.
The attack comes days after al-Shabab attacked a base housing Ugandan forces from an African Union peacekeeping mission in Bulamarer, 130km (80 miles) southwest of the capital.
Since 2006, the rebels have been trying to topple Somalia’s central government and establish rule based on its strict interpretation of Islamic law.
A government pushback last year put the group on the back foot, but al-Shabab has launched significant attacks against hotels, military bases and government establishments.
The group said in a statement it had killed 73 soldiers in the attack, targeting those who had returned from training in Eritrea.
Al-Shabab tends to give casualty figures in attacks that differ from those issued by the authorities.