The young boys were sleeping in the school dormitory when the fire began overnight, said police spokesman Moses Carter, adding that an electrical fault could have caused the blaze.
President George Weah visited the site in Paynesville, on the outskirts of the capital, and said the cause was still unknown.
The pupils who died were aged between 10 and 20, according to a presidential spokesman. The police spokesman said 27 students had been killed.
“We are here to encourage parents of the victims to have strength, because it is painful to lose your kids in this manner,” Weah told reporters at the scene.
“We extend our sympathy to the bereaved families. We don’t know the cause of the fire yet, but we will encourage our investigators to find how it happened,” he added.
Rescuers in white masks and surgical gloves carried the children’s bodies in bags from the burnt-out building as crowds of people and relatives crushed together outside.
The sheet-metal roof of the building, which housed a school and boarding school, was destroyed.
“Our team is investigating the cause of the fire,” the police spokesman said. “It may be electrical,” he added, while refusing to rule out the possibility that the fire was a criminal act.
– The children were asleep –
“I was sleeping when I heard noise outside. My wife opened the back door and we saw smoke coming from the front. We came out and saw fire at the back,” said local resident Zazay. Ballah, who said they had helped in the rescue efforts.
“We went for water, trying to put it out. We were putting water up to 2.30 am. When the fire fighters came, the fire was already going down.”
The fire struck while the children were asleep, said Fulani community official Amadou Sherrif.
The victims were buried swiftly in a collective ceremony, in the Muslim tradition.
In an earlier tweet, Weah offered condolences to the families of those affected.
“My prayers go out to the families of the children that died last night in Paynesville City as a result of a deadly fire that engulfed their school building,” he wrote.
“This is a tough time for the families of the victims and all of Liberia.”
The Liberian authorities are all too familiar with deadly fires, often caused by malfunctioning generators, though “not on this scale,” the presidential spokesman said.
The majority of Liberians are Christian but there is a sizeable minority of Muslims among the 4.5 million population.
Liberia is one of the least developed nations in the world, and its recent history has been blighted by civil wars and the deadly Ebola virus.