QUITO, Ecuador — More than 60 inmates were killed Tuesday in the worst prison riots in Ecuador’s history, as rival gangs battled for control of the country’s growing drug trade.

The violence broke out in a series of coordinated mutinies on Tuesday morning in three large prisons across the country, according to the police. It was not until the afternoon that the authorities regained control.

Videos recorded by inmates and shared on social media showed beheaded corpses and mutilated arms and legs, shocking a nation unused to massacre. The ghastly imagery made clear just how far Ecuador has fallen into the violent spiral of organized drug crime.

“This sort of thing was unthinkable in our country,” Ricardo Camacho, who once headed Ecuador’s prison system, said in an interview. “This is a tragedy, a true shock.”

The government said Tuesday’s attacks were part of a feud between rival drug gangs.

In December, the leader of a prominent local gang called Los Choneros was assassinated in a shopping mall in the port city of Manta, which has become an important hub for cocaine trafficking to Central America.

On Tuesday, the battle moved to the prisons as Los Choneros members retaliated for their leader’s death, said Gen. Edmundo Moncayo, the head of Ecuador’s prison system. Many of the victims, he said, were not tied to organized crime but simply caught up in the battle.

“Two armed groups tried to seize the criminal leadership of the detention centers,” General Moncayo said.

Although Ecuador does not itself grow large quantities of coca leaf, it is flanked by the world’s two largest producers, Colombia and Peru.

Colombian cocaine traffickers and guerrillas have long used Ecuador’s territory for operations, and in recent years began diverting a growing share of exports to neighboring countries, as the Colombian authorities stepped up controls at ports and airports.

Ecuador’s overcrowded prisons have become increasingly violent over the past three years, as drug gangs gained effective control.

The violence worsened after prisons were forced to cut their budgets under an austerity program adopted by Ecuador’s financially struggling government, said Daniela Oña, who studies human rights abuses in Ecuadorean jails.

“It is a multidimensional problem,” Ms. Oña said, noting that there is now “less money for psychology, sports, culture, social work — all these factors that prevent a proper social rehabilitation.”

In December, five inmates died in a prison brawl among members of a local drug gang, according to the police. In 2019, two dozen Ecuadorean inmates died in a series of mutinies, with two victims burned alive.

José María León Cabrera reported from Quito, and Anatoly Kurmanaev from Caracas, Venezuela.