Most were killed in Bakara, Mogadishu's biggest market, when African Union troops started shelling the area after an explosive device hit one of their convoys, witnesses said.
"The medical teams got an emergency alert and they rushed to Bakara market where they have collected around 16 dead bodies of civilians who were killed in the shelling," said Ali Muse, head of Mogadishu ambulance service.
Another five civilians were killed in crossfire after a roadside bomb and an ambush hit an African Union convoy, witnesses said.
Muse said at least 46 civilians were injured in the latest violence.
"We heard a heavy exchange of automatic weapons and a blast and then we found ourselves under mortar fire," a trader at the market, Abdirahman Hussein, told AFP.
"At least nine mortar bombs hit the areas in the market where people sell vegetables and gold," he said, adding: "I personally saw nine dead bodies in two different spots but other civilians were killed elsewhere in the market."
African Union officials confirmed the attack on their convoy but said they had no information about civilian casualties.
"There was an ambush attack that targeted our convoy today. They fired RPG?s and machine guns onto the peacekeepers and two of them suffered minor injuries," Captain Prosper Hakizimana, a spokesman for the African Union troops, told AFP.
The ambush appeared to follow the explosion of a bomb that witnesses said was hidden under a pile of rubbish. The blast was near the Jalle Siad military academy.
One African Union vehicle was damaged in the attack, witnesses said.
"I believe there were casualties on the side of the attackers although we could not establish how many," Hakizimana said.
"The attack occurred in the only road that links the area to the rest of the city and there were civilian buses around the area when the explosion hit the convoy. Five civilians were killed in the crossfire," said Ali Muhidin, a witness.
"Some of the civilians were trapped and killed inside a minibus that was passing by the area when the gunfight broke out after the explosion," said Amin Ahmed, another witness.
Calm had returned to Mogadishu by late afternoon, residents said.
Radical Shebab Islamists, linked to Al-Qaeda, have vowed to overthrow Somalia's transitional government, which controls only a tiny portion of Mogadishu and that is only thanks to the presence of the some 7,500 Ugandan and Burundian troops that make up the African Union force.