PAYNESVILLE, Liberia — A fire at an Islamic school outside the Liberian capital has killed at least 27 people, the police there said on Wednesday, adding that it was likely that many of the victims were children.

The fire, which started around midnight, gutted a dormitory and school building where students slept about seven miles east of Monrovia, said a police spokesman, Moses Carter. Only the imam and two students escaped, he said. The cause of the fire was under investigation.

Hundreds of grief-stricken people stood in shock as ambulances took away the bodies. Others wailed and wept as the police pushed back a curious, surging crowd.

The Liberian president, George Weah, visited the scene and was expected to attend the victims’ funeral at a Monrovia mosque before burial later in the day, said his spokesman, Isaac Solo Kelgbeh.