Domingos Coelho counted 180 knife wounds on the front of his son Dyego's headless torso. He couldn't bring himself to roll the body over and count those on the back.

Dyego Coelho, 21 when he was killed last month, was in prison for illegal possession of ammunition, his father told reporters – a minor crime for which he likely would never have been sentenced to jail time, but in Brazil's backlogged and decrepit criminal justice system, he was locked up with long-term prisoners until he saw a judge.

He was jailed in Pedrinhas, a prison complex in his home state of Maranhao that has nearly double the number of inmates it was built for and where dozens of prisoners are killed each year. On Dec. 17, in the course of a riot, gang members killed members of a perceived rival gang, something they were able to do with grim efficiency in the prison, and they separated the body of Mr. Coelho – and those of two relatives and fellow inmates of his – from their heads, to make a point about their unchecked power.

Story continues below advertisement

Gang members filmed the deaths on a cellphone (an item they are, in theory, prohibited from having in prison) and then used another prohibited technology to share the video; it made its way to the newspaper Folha de Sao Paulo, which published it earlier this month. That, and a judicial report on the riot, evoked condemnation from Brazilian and international human-rights organizations, which prompted the Governor of Maranhao to reply she was happy that the violence in her state stays within prison walls. Brazilian President Dilma Rousseff said nothing about any of it for nearly a week, then finally tweeted the news that she was setting up a task force to respond to the crisis and moving a few men perceived to be ringleaders in the violence to federal prisons.

Yet two more prisoners have died in Maranhao in the past week, the latest one suffocated and carved into pieces, his head left in a garbage bin – retaliation, police say, for the removal of the gang leaders. When police went after weapons in Pedrinhas on Wednesday, prisoners responded by setting their mattresses on fire and creating yet another crisis.

The near-daily horror stories from Pedrinhas have dominated the headlines, but those who know Brazil's prisons say it could be happening in any one of a hundred places.

"The images are terrible and the level of cruelty is making us feel terrible – but every single year we have something like this happening in Brazil," said Lucia Nader, director of Conectas, a human-rights organization, adding that the conditions exist "for this to happen in any prison in Brazil."

Brazil's economy has surged in recent years to become the world's sixth largest; social policy reforms have helped create a huge new middle class, and the country is keen to flex international muscle, gunning for a UN Security Council seat. But in a few critical areas, this country retains institutions more typical of a developing nation. "There are two areas, the police and the prison system, that haven't gone through the growth and transition of the rest of the country – and they are still in the 18th century," Ms. Nader said.

There is no political constituency with any inclination to reform the prison system, said Herbert Carneiro, a judge who heads the National Council on Criminal and Penitentiary Policy. Polls show a majority of Brazilians believe less money should be spent on prisoners, that sentences should be longer, that the age of incarcerating adolescents as adults should be lowered.

There are "no consequences" for the federal or state governments when prisoners are killed, or live 16 in a cell built for two, Mr. Carneiro said – and there may be incentive to maintain that status quo.

Story continues below advertisement

Brazil has 555,000 prisoners – the third-largest incarcerated population in the world – in prisons built for 300,000. These facilities are wildly violent. In Sao Paulo alone, for example, the national database says that 35 prisoners died in 2011, but an audit by the arms-length Prison Pastoral Service found that there were 500 deaths. Prisoners are poorly fed; almost none have access to the education and work programs that are mandated by Brazil's penal law, which also says prisoners must each have their own cell – a condition that exists almost nowhere in the country.

The primary problem with the prisons, said Julita Lemgruber, the former head of prisons in the state of Rio de Janeiro, is that "half of the people in them shouldn't be there." Forty per cent of Brazilian prisoners have not been sentenced; an additional number have actually completed their sentences but do not understand the penal system, which fails as a matter of course to release them. Almost everyone arrested relies on a public defender; Sao Paulo, a city of 16 million people, has 300 of them.

Once incarcerated, they need to survive. And the prisons are firmly in the control of gangs. Many of these began as prisoners' organizations almost like unions, explained sociologist Camila Caldeira Dias, and even today they operate as welfare groups that try to compensate for state neglect by providing basics such as soap and toilet paper. But now they are also criminal organizations – the First Capital Command, known by its Portuguese acronym PCC, which controls most of Sao Paulo's prisons, operates a drug empire that now stretches into Paraguay and Bolivia – and it rules through brutal violence.

The judicial commission investigating Pedrinhas found that its members could access whole portions of the prison only at the invitation of inmates – prison employees could not set foot there – and that gang leaders were raping the partners of prisoners who came on conjugal visits.

"The state has no control at all of its prisons," Mr. Carneiro, the prison policy chief, said bluntly. "It controls only our fake feeling of public security."

After the Pedrinhas video was leaked, the state government finally sent an elite police force to try to retake control of the prison; in response, gang leaders ordered retaliation outside prison walls, in the state capital of Sao Luis. A police station came under machine-gun fire and buses were set ablaze – a six-year-old child on one of the buses died of burn injuries.

Story continues below advertisement

The Brazilian state forfeited control of its prisons decades ago, says Prof. Dias, author of a definitive study of prison gangs. The country has a mass-incarceration policy instituted in the 1990s to respond to a violent drug war; people found in possession of small amounts of drugs for personal use are jailed at the same pace as dealers (if they can't afford a lawyer), causing the prison population to balloon.

"We have a society that only punishes poor, black criminals, who don't have any money to pay for lawyers, who don't have the right to fully defend themselves," she said. "The prisons are the spaces where you segregate and contain this population, which historically, in Brazil, has no rights. … They didn't have rights out here. And in prison there is no concern about violating these rights."

While the current left-leaning government speaks of rights more often than its predecessors, it makes political deals with entrenched conservative interests that block penal reform, she added. Maranhao, for example, has been controlled for 50 years by the Sarneys, one of Brazil's most powerful political families.

"There are great interests, from governments, to maintain the system as it is," said Valdir Silveira, a Roman Catholic priest who is the national co-ordinator of the pastoral service and recently audited conditions in Maranhao. Powerful politicians have close ties to the companies that provide prison services (and run those that have been privatized), while much of the prison budget never makes it to administrators, he said.

Brazilian Minister of Justice Jose Cardozo, who in 2012 called the country's prisons "medieval" and said he would prefer to die rather than be incarcerated in the system he supervises, recently pledged $510-million to the penal system, but only for building new prisons.

Ms. Lemgruber, the former Rio prisons chief, said the single most valuable change would be keeping people out of prison in the first place – once shoplifters are put in cells with murderers, and have to join gangs to survive, they are entrenched in criminal life, she said. Brazil needs a real system of alternative justice, better sentencing, intelligent segregation of its prison population and a legal system that provides judicial process, not warehousing. "It would be a good thing if our prisoners were treated like animals," she said. "That would be an improvement."