RIO DE JANEIRO — At least 57 people were killed in a prison riot in northeastern Brazil on Monday, authorities said, the second time since May that a clash between rival gangs exploded into a horrifying bout of violence.

At least 16 of the victims were decapitated, authorities said. Others were asphyxiated.

It took five hours for prison officials to quell the riot at the Altamira jail in Para state. The clash erupted early Monday after members of one gang infiltrated another part of the jail. Video and pictures aired by local media showed flames shooting out of the prison.

Two prison officials were taken hostage but were reportedly freed unharmed. The gangs were the feared Comando Vermelho — the Red Command, whose base of power is Rio de Janeiro — and Comando Classe A.

AD

“The state government will identify the leaders of the two factions involved in the confrontation so that we can put them in federal prisons,” Helder Barbalho, the Para governor, said Monday afternoon.

AD

The clash echoed a riot in northwestern Brazil in late May, when a power struggle involving the Northern Family, Brazil’s third-most-powerful gang, led inmates to strangle or stab at least 55 people , some in front of visiting families.

The violence illustrated the mounting insecurity in Brazil’s notoriously underfunded and overcrowded prisons — and the challenge facing officials as they struggle to restore order to the system. Brazil’s inmate population has swelled from about 500,000 inmates a decade ago to an estimated 800,000.

AD

President Jair Bolsonaro, elected last year on promises to crack down on violent crime, has described plans to “stuff prison cells with criminals.”

“I’d rather a prison cell full of criminals than a cemetery full of innocent people,” he said. “We will build more if necessary.”

AD

The Altamira jail was already over capacity, the National Justice Council reported this month. It was built for 163 detainees but housed 343. The council report called conditions inside the prison “terrible.”

Unconfirmed images and video on social media appeared to show chaos at the prison, with black smoke billowing out from inside the prison walls. People can be seen running around inside. One video appeared to show decapitated heads piled up against a wall beside a man waving a machete.

AD

Politicians seized on the bloodshed as more reason to crack down on criminals.

“In these fights between criminal factions, I root for the machete,” said Gilson Cardoso Fahur, a congressman from Parana state, one of many politicians who recently won office on the promise to get tough on crime in a perennially violent country.

AD

The deteriorating conditions at the country’s prisons are one result of Brazil’s years-long war on crime. Some prisons are so overcrowded that inmates sleep standing up, with hands tied to bars to keep them upright.

Analysts worry that the current conundrum in the system — too many people, too little funding — is a deadly one.

AD

“There are almost twice as many prisoners as there is prison space,” said Bruno Paes Manso, a researcher the University of Sao Paulo’s Violence Studies Center. “The result is that this insecurity strengthens gang command and prison leadership, which has grown in Brazil in the last 10 years.”

He predicted that extraordinary prison violence will not only continue in the coming years but worsen, too.

“By insisting on high incarceration in overcrowded prisons, Bolsonaro wants to increase the dose of the poison that is killing us,” he said.

AD

For the past four years, Brazil’s largest gangs have been waging a turf war that has spread across the country. Increasingly profitable drug routes have caused the country’s largest gangs to expand their presence, form alliances with smaller gangs and challenge rivals. Clashes led to record-high homicide rates throughout the country in 2017.

AD

Many of these gangs see prisons, which are effectively run by gang lords, as their command centers. They use smuggled cellphones and videos to give orders and set rules. Inmates are forced to pledge allegiance to survive. Deadly prison riots are increasingly common, as leaders seek to cement their power through extreme violence.

Many prisons are controlled at the state level, so the Bolsonaro administration has called on states to build more prisons. That has so far seemed unlikely, however, given funding challenges.

AD

Another proposed solution has been to move gang lords to federal penitentiaries.

“Immediately, we will open spaces in federal prisons to isolate those responsible for the barbarity,” Justice Minister Sérgio Moro said after Monday’s riot. “In my opinion, they should stay in federal prisons forever.”

AD

The Brazil office of Human Rights Watch has investigated the country’s prisons. Maria Laura Canineu, the organization’s Brazil director, called the violence the most visible sign of a structurally defective system.

“Nationwide, 40 percent of people in detention are awaiting trial,” she said. “Most inmates are black young men with low levels of education who depend on understaffed public defenders’ offices for their legal defense.”

She said she expects prison gangs to strengthen as the facilities are stuffed with more inmates, many of whom were already gang members — or soon will be.

AD

“It is really sad to see this [violence] again,” she said. “But not surprising, unfortunately.”

AD