In seconds, wooden beams and tin sheets came crashing down on the young pupils, as some of the walls gave way.
"You can understand how it feels when many students, mostly children, came under attack as school rooms collapsed," Abdi said.
People pulled injured victims out of the rubble, using their bare hands to carry them to safety until ambulances arrived.
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.
The founder of Mogadishu's Aamin ambulance service, Abdulkadir Abdirahman Adan, 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 and a colleague seriously wounded in a suicide bombing claimed by Al-Shabaab.
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 25 September 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.