Brazil Prisoner beheaded
Three prisoners were killed during clashes between rival gangs at the Pedrinhas prison in Maranhao, BrazilYouTube/Folha de Sao Paulo

A gory video showing the beheaded bodies of two inmates lying in a pool of blood on the floor of a Brazil prison has highligted the lawlessness affecting some of the country's jails just months before the World Cup.

The video published by local media was taken by a detainee after clashes between two rival gangs inside the infamous Pedrinhas prison in Sao Luis, in the northeastern Maranhao state.

Daily Folha de Sao Paulo uploaded it on to its website, after receiving it from a union representing employees of the state penitentiary system.

Three dead men showing numerous stab wounds and signs of torture are stacked up against each other in the prison yard. The severed heads of two of them rest on top of one's bare back.

Folha identified the victims as Diego Michael Mendes Coelho, 21, Manoel Laércio Santos Ribeiro, 46, and Irismar Pereira, 34. The video was recorded on December 17.

The overcrowded Pedrinhas prison is notorious for gang warfare and riots.

Some 62 detainees were killed inside the prison walls in 2013.

Thirteen died in a single outbreak of violence in October. Built for 1,700 inmates, the detention facility holds more than 2,100.

A judicial report into Pedrinhas drafted at the end of December detailed cases of torture, retribution killings and sexual abuses. It said prison authorities had lost control of the facility.

According to the report, female visitors were also forced into having sex with gang leaders.

"The relatives of the powerless prisoners inside the jail are paying this price so that they won't be murdered," Judge Douglas Martins told Brazil's judicial news agency. "It is a serious violation of human rights."

Brazil, which hosts the football world cup this summer, has the world's fourth largest prison population.

However, its detention facilities reportedly have space for only 300,000 of the country's half a million inmates.