The official said the bodies were piled on top of each other, indicating a possible stampede as people tried to flee the fire.

Malaysian authorities were investigating the cause of a fire early Thursday that blocked the only exit of an Islamic dormitory on the outskirts of Kuala Lumpur, killing 23 people, most of them teenagers.

Firefighters and witnesses described scenes of horror first of boys screaming for help behind barred windows as neighbors watched helplessly, and later of burned bodies huddled in corners of the room. Islamic teacher Arif Mawardy said he woke up to what he thought was a thunderstorm, only to realize it was the sound of people screaming.

Firefighters rushed to the scene after receiving a distress call at 5-41 a.m. and took an hour to put out the blaze, which started on the top floor of the three-storey building, Kuala Lumpur police chief Amar Singh said.

Mr. Singh said 23 charred bodies were recovered 21 boys between the ages of 13 and 17 and two teachers.

“We believe [they died of] suffocation ... the bodies were totally burnt,” he said. Mr. Singh said 14 other students and four teachers were rescued.

The fire broke out near the only door to the boys’ dormitory, trapping the victims because the windows were barred, fire department senior official Abu Obaidat Mohamad Saithalimat said. He said the cause was believed to be an electrical short-circuit, though Mr. Singh said the investigation was continuing.

Another fire department official, Soiman Jahid, said firefighters heard shouts for help when they arrived at the school. He said they found a pile of bodies in the right corner of the dorm and another pile in the left corner.

Local media showed pictures of blackened bed frames in the burned dormitory. A resident, Nurhayati Abdul Halim, was quoted as saying that she saw the boys crying and screaming for help.

“I saw their little hands out of the grilled windows; crying for help. ... I heard their screams and cries but I could not do anything. The fire was too strong for me to do anything,” she said. She added that the school had been operating in the area for the past year.

Noh Omar, Malaysia’s Minister for Urban Well-being, Housing and Local government, said the school’s original architectural plan included an open top floor that allowed access to two exit staircases. But he said a wall had been built dividing that floor, leaving only one exit for the dorm.

“The wall shouldn’t have been there,” he said. He added that the school submitted an application for a fire safety permit that hadn’t been approved.

The school, Darul Quran Ittifaqiyah, is a private Islamic center, known as a “tahfiz” school, for Muslim children, mainly boys, to study and memorize the Koran.

School principal Mohamad Zahid Mahmod was quoted by the Berita Harian newspaper as saying the students were being housed in a temporary building because of renovation work at the main school building. He said they were due to move back at the end of this month.

Mohamad Zahid said the school has been operating for 15 years and is registered with the state Islamic religious council. He said the school had housed 42 students, six teachers and two wardens.

The Star newspaper said there were 519 tahfiz schools registered nationwide as of April, but many more are believed to be unregistered. Many such schools are exempt from state inspections.

The newspaper said the fire department has recorded 211 fires in such private Islamic centers since 2015. In August, 16 people fled a fire at a tahfiz school in northern Kedah state. Another tahfiz school was destroyed by a fire in May but no one was hurt.

The worst fire occurred in 1989 when 27 female students at a private Islamic school in Kedah state died in a blaze that gutted the school and eight wooden hostels.