The official, who spoke on condition of anonymity because he was not authorized to speak to the media, said special forces raided the camp in Jame'o village in Middle Shabelle region. A local commander was among those killed, he said.
A second official confirmed the raid, which was carried out with the support of helicopters that later evacuated the young recruits.
Human Rights Watch earlier in the week accused al-Shabab of the forced recruitment of hundreds of children in recent months. The recruitment of children is a long-standing practice of the al-Qaida-linked group which faces growing military pressure across south and central Somalia.
Thirty boys were rescued in the overnight raid, Somalia's information minister, Abdirahman Omar Osman, told The Associated Press.
"Al-Shabab bas once again demonstrated their barbarity and their complete disregard for human rights," the minister said. "The group uses these indoctrination camps to brainwash young men and force them to conduct attacks and suicide bombings." He said Somalia's military was making "significant progress" in retaking territory from the extremist group.
Also on Friday, the U.S. military said it had carried out an airstrike in Somalia that killed four members of the al-Shabab extremist group.
A statement from the U.S. Africa Command said the strike was carried out Thursday about 50 kilometers (31 miles) northwest of the port city of Kismayo. The statement said no civilians were killed.
The U.S. military carried out more than 30 drone strikes last year in the long-chaotic Horn of Africa nation after President Donald Trump approved expanded military efforts against al-Shabab.
The extremist group was blamed for the October truck bombing in Somalia's capital, Mogadishu, that killed 512 people. Thursday's U.S. airstrike was the first since one early this month that killed two al-Shabab extremists and destroyed a vehicle carrying explosives, "preventing it from being used against the people in Mogadishu."
Last year, Somalia's Somali-American president vowed that his government would drive the extremist group out of the country.
Follow the AP's Africa news on Twitter at www.twitter.com/AP_Africa
Copyright 2018 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.