The blaze broke out before dawn in the tahfiz -- an Islamic religious school -- in the heart of the capital Kuala Lumpur.
Firefighters rushed to the scene and the blaze was out within an hour but not before it wreaked terrible devastation.
Pictures in local media showed ash-covered, fire- blackened beds, as horrific accounts emerged of the youngsters trying to escape the school as it went up in flames and neighbours hearing their cries for help.
"There were metal grills which prevented them from exiting the burning building."
More From This Section
Kuala Lumpur Police chief Amar Singh said that "the bodies were totally burned".
"Unfortunately there was only one entrance, so they could not escape. All the bodies were found lumped on one another."
The Star newspaper reported that people in the area who had woken for morning prayers heard cries for help and saw flames engulfing the top floor of the building, where children were sleeping in dorms.
Officials initially said 23 students and two teachers were killed in the blaze. Police later revised down the death toll to 22 students and two teachers.
Six other students were in hospital in critical condition, police chief Singh said, while a handful escaped unhurt.
He said the victims who were students were all boys aged between 13 and 17.
Minister Tengku Adnan said the religious school, called Tahfiz Darul Quran Ittifaqiyah, had been operating without a licence, while local media reported that officials had recently raised fire safety concerns about such private schools.
"There are many other religious schools (that operate illegally) in the country."
Tahfiz are religious schools where children study the Koran in Malaysia, where over 60 percent of the population of about 30 million are Muslim.
The fire and rescue department had raised concerns about fire safety measures at unregistered and private tahfiz, and had recorded 211 fires at the institutions since 2015, according to the Star.
In August, 16 people including eight students fled an early morning fire at a family-run tahfiz in Baling, in the northern state of Kedah, the paper reported.
Religious schools have been under heightened scrutiny since an 11-year-old boy died after allegedly being beaten last year at one of the institutions in the southern state of Johor.
While major fires are rare in Malaysia, they do occur occasionally.
In October last year, six people died in a fire that swept through the intensive care unit of a major hospital in the southern state of Johor.