It was the second attack this week on a mosque in Bakara Market, an area of Mogadishu dominated by members of the country's two main rebel groups, al Shabaab and Hizbul Islam.
Saturday's attack was at the Abdalla Shideye mosque which is used by al Shabaab officials to deliver speeches. Most of the dead were said to be members, officials and commanders of the hardline rebel group.
Relatives bent over dead bodies in a corner of the mosque below walls pockmarked by shrapnel. Outside, sheets of cardboard covered other victims and the wounded were pushed on wooden wheelbarrows to awaiting ambulances.
Some witnesses said a senior leader of al Shabaab, Fuad Mohamed Khalaf, was the target. Khalaf, also known as Fuad Shongole, is on a list of people in Somalia subject to U.N. sanctions because of their involvement in the conflict.
"Thirty-two people died and more than 70 were wounded in the attack," Sheikh Mohamed Ibrahim Bilaal, a senior al Shabaab official, told Reuters. "Sheikh Fuad suffered wounds on the hands. The rescue workers are still taking people to hospitals."
Nurses at Daynile hospital said seven of the badly wounded subsequently died, taking the death toll to 39.
According to the U.N. Security Council, Khalaf has raised funds for al Shabaab and was involved in two car bomb attacks in the capital in April 2008.
The mosque attacks are a new phenomenon in the Horn of Africa nation that has been plagued by violence and anarchy since 1991.
It was not clear who was behind the latest blasts. Residents suspected they could be the result of infighting between the insurgent groups, which are both trying to topple the Western-backed government.
Information Minister Dahir Mohamud Gelle denied any government involvement, telling Reuters it would not attack the rebels where many other people were gathered.
Al Shabaab has been fighting the Western-backed government of Somalia since the start of 2007. It now controls much of southern and central Somalia and has hemmed the government into a few blocks of the capital.
Hizbul Islam is also fighting to topple President Sheikh Sharif Ahmed's administration. While the two groups have fought together against government troops in Mogadishu, they have clashed on numerous occasions in the south of the country.
The government has been promising for months to launch an offensive against the rebels to regain control of Mogadishu.
Some residents said they feared the strikes were just the latest tactic copied by Somali insurgents from Iraq, where Sunni and Shia insurgents often target each others' mosques.
Al Shabaab has also used suicide bombers to devastating effect over the past two years, killing five government ministers and dozens of African Union peacekeeping troops.
(Additional reporting by Ibrahim Mohamed, Feisel Omar and Omar Faruk in Mogadishu and Sahra Abdi in Nairobi; Writing by David Clarke; Editing by Maria Golovnina)
Sheikh Fu'ad Mohamed Khalaf, better known as Fu'ad Shongole, Shebab's head of mobilisation on a local radio. somalia