Officials suspected an electrical short circuit caused the blaze that broke out in a top floor dormitory, where most of the students perished.
The fire at Darul Quran Ittifaqiyah, a "tahfiz" boarding school where students learn to memorise the Koran, was reported around 5.40 a.m. local time (2140 GMT Wednesday), according to a statement from the Malaysian Fire and Rescue Department.
The officials initially gave a death toll of 25 people, but later revised it to 22 students and 2 wardens.
The blaze began in the sleeping quarters on the top floor of the three-storey school building, the statement said.
Kuala Lumpur police chief Amar Singh told reporters the boys who died were aged 13-17, and that they were probably suffocated due to smoke inhalation.
The dormitory had only one entrance, leaving many of the victims trapped inside, he said. Some witnesses said they had heard the students crying for help after the fire broke out.
"They're still counting the bodies, which were piled on top of each other in a corner," Singh said.
Hundreds of people, including families of some victims were gathered outside the school, as more bodies were being removed by fire officials.
The police chief said no foul play was suspected.
Abu Obaidat bin Mohamad Saithalimat, deputy director of the fire department told reporters outside the school that the fire was likely caused by short circuit.
Seven people were taken to a nearby hospital for injuries, while 11 others were rescued, officials said.
Tahfiz schools, which are unregulated by the education ministry and fall under the purview of the religious department, have been under scrutiny since earlier this year when an 11-year-old boy died after reported abuse in Johor, north of Singapore.
(Reporting by Rozanna Latiff and A. Ananthalakshmi; Editing by Praveen Menon and Simon Cameron-Moore)
(This story has not been edited by NDTV staff and is auto-generated from a syndicated feed.)