Three children from Vietnam who were trafficked to the UK and forced to work for criminal gangs have had their criminal convictions quashed.
The children were arrested after police raids on cannabis factories and later convicted of drug offences.
The judge said they were victims of a "vile trade in people" and should not have been prosecuted.
The Crown Prosecution Service said it would not have bought charges if it had had all the evidence at the time.
The Court of Appeal overturned the convictions and issued guidance to courts about how potential trafficking victims should be treated by the criminal justice system.
Barrister Parosha Chandran, a lawyer for one of the victims, said the cases "represent miscarriages of justice".
"They were prosecuted, convicted, and punished by imprisonment for the very crimes that their traffickers forced them to commit or they were compelled to commit as a result of the trafficking."
One of the children, now 18, said he had been brought to England in a freezer container. In 2011 he told police he was "relieved to see them" when he was arrested at a house in Bristol where cannabis was being grown.
The court found his criminal activities were "integral" to his status as a trafficked child.
Another victim, 18 at the time, was caught tending to cannabis plants in Harrow in 2009 after escaping from the care of Kent County Council two years earlier.
He was sentenced for two years in a young offenders institution.
The third was sentenced to eight months' detention after he was found barefoot by police near to a house full of cannabis plants.
He admitted looking after the crop but said he did not know it was illegal.
Neighbours had reported seeing the 16-year-old being taken from the house with his hands bound by a group of men in Mansfield last March.
Lord Judge, Lord Chief Justice, said: "This vile trade in people has different manifestations.
"Whether trafficked from home or overseas, they are all victims of crime. That is how they must be treated and, in the vast majority of cases they are, but not always.'"
The guidance issued in the wake of the case makes it clear it is not for the courts to decide whether someone should be prosecuted.
But in cases where issues of trafficking arise, the court can stop the prosecution if it is thought the defendant is a trafficking victim and committing offences as a result of their exploitation.
In an entirely separate case, heard at the same time, a Ugandan woman in her mid-30s also had her conviction overturned. She had been sentenced to six months in prison in 2011 after pleading guilty to possessing a false passport.
But the court heard she was suffering from complex post-traumatic stress disorder after "prolonged exposure to involuntary prostitution and enforced control" and that the passport offered her the prospect of escape.
The CPS said its policy clearly stated that "very careful consideration" should be given before charges are brought against victims of trafficking.
It added: "In light of the fresh evidence received in all of these cases the CPS did not resist these appeals.
"Had this evidence been available at the time of the decision to prosecute the CPS would not have prosecuted."
It said the judgement had highlighted a number of issues around identifying and communicating when a defendant may be a victim of trafficking, adding that more needed to be done "to ensure every part of the criminal justice system plays their part".
The Association of Chief Police Officers said it is working with the CPS on new guidance, but a spokesman admitted it was "challenging" to determine whether those committing crimes were victims of trafficking.
"When the police find people working in cannabis farms, they look for any evidence that proves that the individual is a victim of trafficking," a spokesman said.
"It can be difficult to gather this evidence. Traffickers have such control over their victims that many victims will not disclose their situation to the authorities."
Speaking after the ruling, Wendy Hewitt from the Equality and Human Rights Commission's deputy director said the judgement would have "a significant effect" on how the criminal justice system treats child victims.
She added: "The court has made it clear the relevant authorities must properly investigate these cases before pressing charges, especially where children are involved."
The charities Anti-Slavery International and ECPAT noted in a statement that the Court of Appeal had recognised the importance of Article 8 of the EU Trafficking Directive which enshrines the right for victims of trafficking to not be prosecuted for involvement in criminal activities.
Klara Skrivankova, Anti-Slavery International's trafficking programme coordinator, said: "We know there are hundreds of cases of men, women and children trafficked into the UK for forced criminality and many of them end up being prosecuted instead of the traffickers.
"This judgement as a milestone in making sure that victims of trafficking are protected against criminalisation."