RIO DE JANEIRO — After the flames were extinguished and guards regained a semblance of control over the prison in northern Brazil where a gang fight left 58 dead, officials encountered a macabre scene.

The severed heads of 16 inmates lay on the concrete floor, surrounded by cigarette butts and puddles of blood. Nearby were another 42 bodies of prisoners who succumbed to smoke inhalation.

The clash between rival gangs at the detention facility in Altamira, in the state of Pará, on Monday was the deadliest outbreak of violence behind bars in Brazil in nearly three decades. But it came as little surprise to experts who have watched the country’s inmate population explode as powerful gangs assert ever more control over the teeming detention facilities.

“Altamira was a tragedy foretold,” said Anna Isabel Santos, a public defender in Pará. “It was a packed prison that hadn’t been upgraded in a decade where two rival gangs were being held side by side.”