Hillary Transue was 14 when she carried out her prank. She built a hoax MySpace page in which she posed as the vice-principal of her school, poking fun at her strictness. At the bottom of the page she added a disclaimer just to make sure everyone knew it was a joke. "When you find this I hope you have a sense of humour," she wrote.

Humour is not in abundance, it seems, in Luzerne County, northern Pennsylvania. In January 2007 Transue was charged with harassment. She was called before the juvenile court in Wilkes-Barre, an old coal town about 20 miles from her home.

Less than a minute into the hearing the gavel came down. "Adjudicated delinquent!" the judge proclaimed, and sentenced her to three months in a juvenile detention centre. Hillary, who hadn't even presented her side of the story, was handcuffed and led away. But her mother, Laurene, protested to the local law centre, setting in train a process that would uncover one of the most egregious violations of children's rights in US legal history.

Last month the judge involved, Mark Ciavarella, and the presiding judge of the juvenile court, Michael Conahan, pleaded guilty to having accepted $2.6m (£1.8m) from the co-owner and builder of a private detention centre where children aged from 10 to 17 were locked up.

The cases of up to 2,000 children put into custody by Ciavarella over the past seven years - including that of Transue - are now being reviewed in a billowing scandal dubbed "kids for cash". The alleged racket has raised questions about the cosy ties between the courts and private contractors, and about the harsh treatment meted out to adolescents.

Alerted by Laurene Transue, the Juvenile Law Centre in Wilkes-Barre began to uncover scores of cases in which teenagers had been summarily sent to custody by Ciavarella, dating as far back as 1999. One child was detained for stealing a $4 jar of nutmeg, another for throwing a sandal at her mother, a third aged 14 was held for six months for slapping a friend at school.

Half of all the children who came before Ciavarella had no legal representation, despite it being a right under state law. The Juvenile Law Centre has issued a class action against the two judges and other implicated parties in which it seeks compensation for more than 80 children who it claims were victims of injustice.

The prosecution charge sheet alleges that from about June 2000 to January 2007 Ciavarella entered into an "understanding" with Conahan to concoct a scheme to enrich themselves. The two judges conspired to strip the local state detention centre of funding, diverting the money to a private company called PA Child Care which it helped to build a new facility in the area.

In January 2002, prosecutors allege, Conahan signed a "placement guarantee agreement" with the firm to send teenagers into their custody. Enough children would be detained to ensure the firm received more than $1m a year in public money. In late 2004 a long-term deal was secured with PACC worth about $58m.

In return, the prosecutors allege, the judges received at least $2.6m in kickbacks. They bought a condominium in Florida with the proceeds. PACC's then owner, Bob Powell, who has not been charged, used to moor his yacht at a nearby marina. He called the boat "Reel Justice".

For a man who has agreed to serve more than seven years in jail as part of a plea bargain, Ciavarella comes across as remarkably unflustered. He invited the Guardian into his Wilkes-Barre home where he remains free on bail pending sentencing.

Though he pleaded guilty to conflict of interest and evasion of taxes, he insists that he took the money in all innocence, assuming it to be a legitimate "finder's fee" from the private company for help in building the detention centre. He denies sending children to custody in return for kickbacks. "Cash for kids? It never happened. People have jumped to conclusions - I didn't do any of these things."

He says that he regarded his court as a place of treatment for troubled adolescents, not of punishment. "I wanted these children to avoid becoming statistics in an adult world. That's all it was, trying to help these kids straighten out their lives."

As evidence, Ciavarella claims the percentage of children he sentenced to custodial placements remained steady from 1996, when he was appointed to the court, until he stood down from it in 2008. Yet the facts suggest otherwise.

For the first two years of his term his rate of custodial sentencing was static at 4.5% of cases. In 1999 - shortly before he allegedly began the racket with Conahan, according to prosecutors - it suddenly shot up to 13.7%. By 2004 it had risen to up to 26% of all teenagers entering his court.

Ciavarella hopes that with good behaviour he may spend only six years in jail.

Hillary Transue, meanwhile, is now 17 and in high school. She spent a month in detention for the parody. For many months afterwards she was ostracised by friends and neighbours, labelled a delinquent.

"It's nice to see him on the other side of the bench," she says of Ciavarella. "I'm sure he understands now how it feels."