One police officer was killed and three others were wounded in the fighting, which began after the mortar landed near the compound during the ceremony, police official Aden Ahmed said.
He said that government soldiers and the African Union forces returned fire, shelling Mogadishu's rebel-controlled district. Most of the shelling hit near the Bakara market, a busy shopping area, Ahmed said.
Ali Musa, the head of the Mogadishu ambulance service, said at least 12 civilians were killed and 15 others were wounded in the retaliation.
Somalia's capital sees near-daily bloodshed as a powerful insurgent group with links to al-Qaida tries to overthrow the fragile government and push out 5,000 African Union peacekeepers.
Somalia has been ravaged by violence since warlords overthrew dictator Mohamed Siad Barre in 1991, then turned on each other. A moderate Islamist was elected president in January amid hopes he could unite the country's feuding factions, but the violence has continued.
Suicide bombings, unheard of in Somalia before 2007, have also become increasingly frequent and the lawlessness has raised concerns that al-Qaida is trying to gain a foothold in the Horn of Africa.
Earlier this month a suicide bomber attacked a university graduation ceremony in Mogadishu, killing 24 people, including three government ministers, medical students and doctors. The government blamed al-Shabab, which has denied responsibility. The group is part of an Islamic insurgency trying to topple the government.