Friday's attack targeted the village of Hilowle Gaab in Hirshabelle State, which Somalia's army and allied militiamen captured from al Shabab last week.
The fighters attacked the village during morning prayers at around 0200 GMT with car bombs, said Hussein Aden, a spokesman for the local clan militia.
A gunfight ensued, during which the militants were repelled, he said, adding that soldiers and militiamen were among the six fatalities.
"We were woken by three deafening blasts on the edge of the village, and then a heavy exchange of gunfire followed," said Mohamed Hussein, a resident of Hilowle Gaab. "Fighting has died down. I do not know how many died."
al Shabab said in a statement that it had recaptured the village and seized military vehicles and weapons, but residents and a local politician disputed that claim.
al Shabab has been waging an insurgency against Somalia's government since 2007, with the aim of enforcing its strict interpretation of Islamic law.
Somalia's government and allied clan militias have forced the militants from large swathes of territory since launching a major offensive last August, but al Shabab has retaliated with a string of attacks, including bombings in the capital Mogadishu.
It killed at least 35 people and wounded 40 more on Wednesday, when it detonated two car bombs in the central Somalia town of Mahas.
It has been pushed back by government offensives in the past, only to regroup and return to areas that the army does not have the capacity to hold.