The fighting began when police executed a plainclothes soldier they suspected of being an Islamist insurgent, police officer Ahmed Nur said. The soldier's colleagues arrived at the scene of the shooting in Benadir market and began attacking the police, he said.
An Associated Press reporter saw 16 bodies at Medina hospital in Mogadishu. Some of the victims were wearing uniforms. Three of the victims were women. Hospital director Mohamed Yusuf said 30 people were wounded.
Cleaners tried to mop up blood from the floor as women screamed outside the hospital. Hundreds of people gathered by the gates, seeking their wounded or dead.
"My husband was killed," wept Hasno Ali. "He was just a businessman."
The killings underscore the lack of discipline and command of Somalia's armed forces after 20 years of civil war. There have been several shoot-outs between police and soldiers in recent years and analysts say the Somali armed forces are more like a collection of militiamen than a well-trained, disciplined national force.
Desertion over lack of pay has been a major problem, impeding the government's ability to fight an al-Qaida linked Islamist insurgency.
Islamist suicide bombers have worn military uniforms before in some of its attacks - a common tactic in conflicts throughout the world - one reason why the police may have been overly suspicious.
Currently international donors led by Italy and the U.S. are supposed to pay around 8,000 Somali soldiers. An EU-led program is halfway through training 2,000 Somali government troops but trainers say one of the biggest problems they faced has been the lack of midlevel junior officers whose duty it is to impose discipline and ensure orders are carried out.