The al-Qaida-linked militants attacked Afgooye town, about 30 km southwest of Mogadishu, Afgooye police chief Abdulkadir Osman said.
"They attacked the town and our forces responded. The fighting lasted a while before our forces defeated them," Osman told reporters, adding that "we are now in full control of Afgooye."
The officer said the militants suffered injuries but did not indicate any casualties on his forces. "Three civilians were also injured in the attack," he added.
Local residents said the fighting caused panic in the town. Bashir Mayow who lived in Afgooye told Xinhua that the clash was deadly.
"Al-Shabaab attacked us last night and the whole town was gripped by fear as Somali forces fought hard to push away the militants," said Mayow.
The al-Shabaab militant group has claimed responsibility for the attack.