Eight civilians were killed and 17 others wounded in the blast, a spokesman for Somalia s police said. AFP
Anguished parents rushed to the scene of the attack, which severely damaged the school building, with injured students taken to hospitals around the city.
Somalia police spokesman Abdifatah Adan said in a brief statement that eight civilians were killed and 17 others wounded, without giving further details.
Security official Mohamed Abdillahi told AFP the explosion was caused by a car bomb, but added: "We don't know the target of the attack."
Witnesses said a large convoy carrying troops from AMISOM, the African Union force fighting the Al-Qaeda linked Al-Shabaab group, was passing through the area when the bomb went off.
"I was close to the area when the blast occurred, there was an AMISOM convoy... passing by," Said Ibrahim told AFP.
Al-Shabaab claimed responsibility for the bombing, saying it was aimed at "military trainers."
The militants frequently carry out attacks in the capital and elsewhere in the country against civilian, military and government targets.
"Everyone was shocked and the scene was really horrible," said Naima Ali, whose daughter was among the lucky ones to escape the carnage unharmed.
"I saw some of the students bleeding," Ali told AFP.
Mohamud Omar, whose cousin was at the school when the attack occurred, said the blast left a trail of devastation in its wake, with backpacks scattered under the debris.
"The blast destroyed the whole area and left the road in ruins, thanks to God that most of the students were unharmed despite the huge damage the explosion caused," he told AFP.
Ahmed Bare, a security guard at a nearby building, told AFP: "The school building was badly damaged and some of their school buses destroyed."
The director of Mogadishu's Aamin ambulance service, Abdikadir Abdirahman, shared photos of the rubble-strewn scene on Twitter, calling the bombing "a tragedy".
- 'Heinous and reprehensible' -
The East African bloc IGAD (Intergovernmental Authority on Development) condemned what it described as a "heinous and reprehensible terrorist attack".
IGAD executive secretary Workneh Gebeyehu said in a statement he "shares the grief of all peace-loving Somalis, and conveys his sincerest condolence to the government and people of Somalia and the families of the victims of the attack".
On Saturday, a prominent Somali journalist was killed in a suicide bombing claimed by Al-Shabaab and a colleague seriously wounded.
Abdiaziz Mohamud Guled, the director of government-owned Radio Mogadishu, was a fierce critic of the Islamists.
Al-Shabaab, which has been waging a violent insurgency against the country's fragile government since 2007, said its fighters had long pursued the journalist.
The militants also claimed two attacks in September that together killed 17 people.
A car bombing on September 25 near the presidential palace killed eight people, including the prime minister's adviser for women and human rights, Hibaq Abukar.
Eleven days earlier, nine people, most of them members of Somalia's security forces, died in a blast near a Mogadishu checkpoint.
Al-Shabaab controlled the capital until 2011 when it was pushed out by AMISOM troops, but it still holds territory in the countryside and launches frequent attacks against government and civilian targets in Mogadishu and elsewhere.