It was just another massacre in a country plagued by violence. But this time it was carried out by prison inmates – who'd been let out specially

It was past midnight and the hired band had launched into a raucous ballad, La Cabrona, to wind up the party. Guests joined in, belting out lyrics in a singsong under a Chihuahuan desert moon: "Dime si ya no me quieres cabrona . . ."

The Italia Inn, a walled compound for rent with a courtyard, kitchen and swimming pool, was a great spot for the fiesta. "Everything was going really well," says Hector, the band's 17-year-old trumpet player.

Nobody heard the vehicles pull up on the dirt track outside or saw the gunmen surround the compound. The first salvo, fired from outside, tore through the garage doors. The band members bore the brunt. Five collapsed in a tangled, bloodied heap. Moments later, the killers stormed into the yard, assault rifles blazing. People screamed and scrambled for cover. Bodies crumpled.

One gunman picked his way through the wounded and taunted them before finishing them off, recalls Hector, who does not want his full name published. "Cry!" the gunman ordered one of the musicians, putting a gun to his temple. "Cry!" The terrified man could only pray. The gunman prepared to fire when a command rang out. "Trabajo hecho, vámonos!" Job done, let's go. The killer lowered his rifle and grinned at the musician. "You're lucky."

Seventeen people died and dozens were injured in the 18 July attack, one of the worst massacres in Mexico's drug war. The crime scene is supposed to be guarded but on a recent morning it was possible to step over the yellow police tape, trodden into the dirt, and pick over the courtyard debris: a scuffed brown shoe, whisky bottles, plates with decomposing sludge. Beer cans bobbed in the stagnant pool and sunlight seeped through 24 raisin-sized holes in the kitchen door. Blood smeared the floor and fridge. Windows were smashed, walls pockmarked. Only the silence was unbroken.

A massacre in Mexico tends to have a short news life. Perpetrators vanish and the deed is eclipsed by the next atrocity, and the one after that. Horrors flow so fast that they lose definition and morph into a single, numbing narrative.

This one was different. When the killers sped away that night it was not the end of the story, but the beginning. The attack set in motion a saga of kidnapping, YouTube video clips, revenge and media blackmail, which exposed a harsh, revealing truth about Mexico in the run-up to this week's celebrations for the 200th anniversary of independence. It is a state colonised by organised crime.

Fly north from Mexico City and the landscape below browns into cauterised scrub. Roads and railway lines, black etchings in caramel plains, eventually converge on a glinting sea of tin-roofed sheds, houses and factories. This is Torreón –"the city that conquered the desert". The first thing you notice is the blinding glare of the sun. The second is a relentless, throbbing heat.

The main drag, Boulevard Independencia, could be Texas: pick-up trucks, gas stations, strip malls, Wal-Mart, Baskin-Robbins. You know the Rio Grande must be close because the coffee – watery americano and only watery americano – sucks. The radio, at least, boasts Latin flavour: upbeat, foot-tapping cumbia music. "For dancing with beautiful women!" smiles the taxi-driver. It is about the cheeriest statement I will hear in Torreón.

The local tabloid, Express, seems to have been written by Dante. Page after page of shootings, stranglings, stabbings, burnings, shallow graves, deep graves, mass graves. Advertisements for spiritual healing compete with those for funeral homes. "Miguel's: best quality coffins at affordable prices." One bright spot is an ad for 600 new jobs to armour-plate cars.

For a country in the throes of a war that has claimed 28,000 lives in four years it is perhaps little surprise that a transport hub such as Torreón, intersection for cocaine, heroin, marijuana and methamphetamines, is grim. Murders among the population of 550,000 average three per day. Two massacres in city bars preceded the attack on the Italia Inn party, a bloodbath made worse by the fact the victims had no connection to drug trafficking.

The atrocity's apparent motive was a display of strength by the Sinaloa cartel in its battle to oust a rival group, the Zetas, from Torreón. "It's a turf war, and they'll kill anyone," says Carlos Bibiano Villa, Torreón's police chief. The day after the attack, the Zetas, keen to show they still controlled the city, left four human heads with a note saying the massacre's perpetrators had been punished. Decapitation, once unheard of in Mexico, has become routine.

The scene of the 18 July massacre in Torreón. Photograph: STR/Associated Press

What came next, however, was new. The Zetas, after killing the four probably random and innocent unfortunates, really did investigate the massacre. The result was a harrowing video uploaded on YouTube. Rodolfo Nájera, bruised, swollen and stripped, gazed into the camera with a confession. The 35-year-old kidnapped policeman, flanked by masked gunmen, must have guessed how the video would end. Asked by an off-camera interrogator about the Italia Inn massacre, Nájera said the killers were Sinaloa members allowed out of prison for nocturnal hits. Guards lent them guns and vehicles. "Who let them out?" barked the voice. "The director," replied the doomed man. The video ends minutes later with a shot to the head.

A tortured confession would hardly be credible except that in this case it was true. The attorney general confirmed the story. Forensic results showed the massacre victims were shot with R-15 rifles – standard issue for prison guards. Federal authorities swooped on the prison and detained the guards. The director, a stout, formidable blonde named Margarita Rojas Rodriguez, who had recently been named "woman of the year 2010" by the state governor, was also arrested. "Disbelief. I just couldn't believe it. I had never heard of something like this," says Eduardo Olmos, Torreón's mayor.

The prison is in Gómez Palacio, a city in Durango state, whereas Torreón is in Coahuila state. But it takes just a few minutes to cross the bridge linking them. Along with the city of Lerdo, they really form one metropolis of just over one million people in a desert bowl that used to be a lagoon. Each state and city has its own police force and jail, a byzantine mess of overlapping institutions and rivalries. It has helped drug traffickers with ample "plomo y plata" – lead and silver, bullets and money – to worm through officialdom like a ripe mango.

From the outside, Gómez Palacio's jail, rising from a dusty plain, looks the part: high white walls, barriers, watch-towers. Officially, it is a "centre for social readaptation", an Orwellian touch. Mothers, wives and girlfriends, the latter in their best jeans and makeup, queue with groceries to get in. The Sinaloa cartel, Mexico's oldest and most powerful, in effect runs the place. A state surrender coyly termed "auto-gobierno", self-government. If you belong to a rival group, odds are you will be carried out in a bodybag. If you cannot pay "cuota", a levy, you sleep outdoors or in a sort of kennel.

Waiting gunmen recently killed three prisoners who had served their time and were leaving the jail on what turned out to be a short walk to freedom. Guards are routinely murdered inside and outside the jail. It is thought Rojas possibly acted more out of fear than greed in allegedly allowing hitmen to borrow guards' vehicles and weapons for nocturnal murder missions.

The next twist came when inmates rioted in protest at Rojas's removal and demanded her reinstatement. The media drove down the one, potholed road leading to the jail to cover the disturbances – and were duly kidnapped: two cameramen from the Televisa network and two reporters from the newspaper group Milenio. The Sinaloa cartel, jealous of the Zetas' YouTube success, demanded that local networks air three of their own videos in return for the hostages.

"This was totally unprecedented. It was brazen blackmail," says one media executive, who asked not to be named. "You couldn't believe these guys were doing this. Things kept reaching new levels of, of . . ." – he searches for the word – "incredibleness." The TV stations broadcast the videos, which turned out to be of frightened police officers accusing colleagues of working for the Zetas. The cameramen and reporters were freed and moved to safe houses in Mexico City. The fate of the police in the videos was unclear.

Javier Garza, sipping Starbucks coffee under a broiling sun, shakes his head. "This is not the place I grew up in." The director of El Siglo de Torreón, the main local newspaper, used to associate the city with progress. Torreón had a bloody role in Pancho Villa's campaign against federal forces in the Mexican revolution but later grew into an economic and industrial hub for ranching, textiles, metallurgy and engineering. It built universities, fountains, a music academy, a championship-winning football team. By the 1990s, when Garza left to study and work in Mexico City and the US, Torreón embodied a newly confident, democratic, thriving Mexico. A hilltop Christ the Redeemer statue, just marginally shorter than Rio's, opened its arms to embrace the city that conquered the desert.

When Garza returned in 2006 to take the reins at El Siglo, local news focused on water scarcity, schools, public works and the football club's battle against relegation. Drugs flowed discreetly north, and flash millionaires built fancy properties, but that was hardly new. Narco-trafficking co-existed with society. "It was peaceful. You could go out and have fun without any problem," says Garza.

That same year, however, things began to change. A drug pusher was shot dead, then a taxi driver, then there was an attack on a wealthy former mayor, the kidnap of a police commander. Homicide rates soared. The same pattern unfolded across much of Mexico. President Felipe Calderón had declared war on the cartels but not anticipated a bloodbath.

Torreón, patrolled by soldiers and police with masks, with shootouts and corpses daily, is enduring violence not seen since the revolution, says Garza. "Instead of being a city of the future, it's like we've closed a circle with the past," he says.

Eduardo Olmos, mayor of Torreón: 'What people tell me is that they want things to go back to the way they were.' Photograph: Rory Carroll for the Guardian

Streets empty after dusk. Staff at the hospital stack corpses for want of space and cower when narcos with AK-47s storm through the wards, seeking rivals. Tens of thousands of Facebook users pledged to attend two protest rallies against the violence but, after rumours of planned attacks, just dozens showed up.

In his city hall office overlooking Plaza de Armas, the mayor, Eduardo Olmos, with a retinue of eight bodyguards, ponders the question of how it all happened. "The police," he sighs. "They came in through the police. They bribed, threatened and recruited them and were able to use their radios, vehicles, weapons, bulletproof vests, everything." By some estimates the cartels have a $100m budget for infiltrating police nationwide. It was a gradual process, says the mayor. "The police relaxed their ethics and discipline and just gave in. In the end they weren't working for them. They were them."

Poverty and unemployment, said Olmos, helped organised crime to recruit and work at street level. "Here the gangs don't hand out free meals like in other cities. They don't have popular support. But there is a lot of tolerance for them. If that turns into support, that will be very dangerous. The only answer is education and employment. And a new police force."

Few would argue with that, but what about legalising drugs? Or allowing one cartel to prevail and restore the era of peaceful co-existence with narco-trafficking? The mayor shifts in his seat. The first option, though backed by thinktanks and at least two Mexican ex-presidents, remains controversial. The second remains taboo, at least officially. "What people tell me is they want tranquility, for things to go back to the way they were," says Olmos, choosing his words carefully. "I may have my own views on the subject, but as an elected official I can't talk about benefitting one cartel or another."

It is alleged that across Mexico some authorities are indeed picking sides in the hope a "winning" cartel or coalition will emerge and end the mayhem. Torreón, at least for now, appears to be betting on a new police force. The city recently fired its entire 1,200-strong force and hired an ex-army general, Carlos Bibiano Villa, to build a new one from scratch. Other cities, notably Ciudad Juárez, have tried that and failed. Villa, however, does not lack confidence. A bear of a man with a moustache and .44 Magnum strapped to his thigh, he keeps a helmet, flak jacket, assault rifle and four walkie-talkies within reach of his desk.

"There were 1,200 police when I arrived and they were all corrupt, the enemy within. I couldn't trust any of them. Now I've got 526 new ones and we're recruiting more." Does he trust them? The general guffaws. "I don't trust my own shadow. That's how I survived 43 years in the army."

Villa, 61, has a PhD in satellite communications but comes across as a wannabe Rambo. With cartels and former police officers gunning for him he sleeps in a small room beside his office where there is another Magnum under the pillow. His family lives in an undisclosed state. He acknowledges geography and economics mean that drugs will always pass through Torreón, yet remains bullish. "We are going to win!" How? "With a hard hand."

Later that night, one of Villa's 12 personal bodyguards is kidnapped and beheaded.

The force's model officer is Raquel Quezada. The 40-year-old mother of two is the sole member of the previous force who passed the vetting and exams. Hollywood would probably dub her the Last Honest Cop. In fact, the former secretary was inspired to sign up by Demi Moore's character being "pushed to the limit" in the film G.I. Jane. On patrol, the soft-spoken Quezada is transformed by body armour, a rifle and skull-painted mask. To prepare for her new job, Quezada ran 10km every day and lost 6kg in gruelling training. "They taught me to control fear and manage risk. This work is dangerous but noble."

Authorities hope to keep the new force honest by promising a free house to every officer who completes 10 years without blemish. A significant carrot, but it is questionable if it can compete with narco threats and cash.

In a different part of the city, a family in a small, pink house makes its own calculations. A dead father and husband. A dead uncle and brother. Three wounded family members. A baby on the way. Funeral and medical bills. It adds up, says Carmen, 37, the eight-months pregnant head of the family and mother of Hector. "I just don't know what we'll do." Hector, who took two bullets, moves slowly and stiffly, a colostomy bag beneath his T-shirt.

Asked if he will play trumpet again Hector shakes his head. "Music, music is . . ." his voice trails off. His mother finishes the sentence. "Music is not really an option any more."