Thirty nine sub-postmasters who were wrongly prosecuted by the Post Office have had their criminal convictions overturned.
The appellants, some of whom were imprisoned for crimes they never committed, had been accused of theft and false accounting because of a faulty computer system.
In court, Sam Stein QC, the barrister representing several sub-postmasters, said the scandal had turned the Post Office "into the nation’s most untrustworthy brand".
Between 2000 and 2014, the Post Office prosecuted more than 700 sub-postmasters, on average one a week, after a bug in the computer system Horizon led to financial shortfalls in branch accounts.
It previously agreed a nearly £58m settlement with 550 sub-postmasters, but most of that money went on legal costs and funders.
Paula Vennells, the chief executive of the Post Office at the time, said in a statement she was "deeply saddened" by what had happened.
"I am truly sorry for the suffering caused to them as a result of the convictions which the Court of Appeal has today overturned," she said.
"I fully support and am committed to co-operating with the ongoing Government Inquiry, as I did with last year’s Select Committee Inquiry."
The barrister who led that civil claim called Friday’s judgment an "incredible victory" that could pave the way for more substantial compensation.
"This is something we should all be ashamed of – that it ever happened, but money will never compensate these people for what’s happened to them," said Patrick Green QC.
The sub-postmasters appealed their convictions on two grounds: that they had been denied a fair trial, and that the circumstances in which the prosecutions went ahead "represents an affront to public conscience".
The court granted the appeal of 39 out of 42 postmasters on both grounds, despite the Post Office fighting 35 of the cases on the second point.
The decision means the postmasters could bring new civil cases for malicious prosecution, which could mean the Post Office paying out significantly more in compensation.
Announcing the court’s ruling, Lord Justice Holroyde said the Post Office "knew there were serious issues about the reliability of Horizon" and had a "clear duty to investigate" the system’s defects.
But the Post Office "consistently asserted that Horizon was robust and reliable", and "effectively steamrolled over any sub-postmaster who sought to challenge its accuracy", the judge added.
Lord Justice Holroyde, sitting with Mr Justice Picken and Mrs Justice Farbey, said: "Post Office Limited’s failures of investigation and disclosure were so egregious as to make the prosecution of any of the ‘Horizon cases’ an affront to the conscience of the court."
Prime Minister Boris Johnson tweeted: "I welcome the Court of Appeal’s decision to overturn the convictions of 39 former sub-postmasters in the Horizon dispute, an appalling injustice which has had a devastating impact on these families for years.
"Lessons should and will be learnt to ensure this never happens again."
One of those whose conviction was overturned was Seema Misra, a former sub-postmistress accused of theft, who said clearing her name was a moment she had been "dreaming about" for over a decade, but added that it "won’t bring back the time".
Mrs Misra took over her post office in 2005 and was pregnant when she was wrongly sent to prison after being accused of stealing £75,000 from her branch.
"I would definitely have killed myself if I hadn’t been pregnant," she said.
Mrs Misra collapsed in court when her original 15-month sentence was read out.
She told Sky News: "Did they even understand the case? Did they know what are they doing? That time I lost faith in justice, I said that’s it, that can’t be done, there’s no justice here."
The mother-of-two said she would never forget the guilt she felt after leaving prison, adding: "It was like I had bought shame to the family.
"My kids are my pride and they didn’t want people to see me and say, ‘Oh, she is Seema Misra’. It was all over the papers and everything, I didn’t want to give them a bad name because of myself."
She described the Court of Appeal verdict as a "huge moment".
When asked if she can now move on with her life, she told Sky News: "I’m not sure I will ever come out of this. I still want to wake up in the morning and say ‘oh it was a bad dream’, I can only hope."
Janet Skinner, who pleaded guilty to false accounting and was sentenced to nine months in prison in 2007, left the Royal Courts of Justice in London to cheers from supporters.
Ms Skinner said she was "relieved" to have finally cleared her name and that to win her case on both grounds of appeal was "amazing".
Asked what her message was to those responsible for the prosecutions of dozens of sub-postmasters, Ms Skinner said: "Watch your backs."
Harjinder Butoy, who was convicted of theft and jailed for three years and four months in 2008, described the Post Office as "a disgrace" after his name was cleared.
Mr Butoy, who was a sub-postmaster in Nottingham, said the traumatic experience had "destroyed my life for 14 years – that’s not going to be replaced".
He said those responsible for the scandal "need to be punished, seriously punished".
He added: "They’re just bullies, that’s all they are… somebody needs to really, really sort this out and charge them for this. It can’t be pushed under the carpet now."
Tom Hedges, who was accused of theft and false accounting and given a seven-month suspended sentence in 2011, opened a bottle of prosecco after his conviction was quashed.
He said: "It’s a wonderful afternoon. When I told my mother, who’s 93, I was coming to court she said ‘get yourself down to Aldi and get some prosecco’.
"She said, ‘just remember your name is Hedges not Rothschild, so get prosecco, not Bollinger!’"
Responding to the judgment, Post Office chairman Tim Parker said: "The Post Office is extremely sorry for the impact on the lives of these postmasters and their families that was caused by historical failures."
Chief executive Nick Read said: "The quashing of historical convictions is a vital milestone in fully and properly addressing the past as I work to put right these wrongs as swiftly as possible, and there must be compensation that reflects what has happened."
In a statement, Helen Pitcher, chairman of the Criminal Cases Review Commission (CCRC), which referred the 42 sub-postmasters’ convictions to the Court of Appeal, said: "This has been a serious miscarriage of justice which has had a devastating impact on these victims and their families.
"Every single one of these convictions has clearly had a profound and life-changing impact for those involved."
She added: "The Post Office has rightly acknowledged the failures that led to these cases and conceded that the prosecutions were an abuse of process.
"We sincerely hope that lessons will be learned from this to prevent anything similar happening elsewhere in the future."
Three of the former sub-postmasters – Wendy Cousins, Stanley Fell and Neelam Hussain – had their appeals dismissed by the court.
Lord Justice Holroyde said in these three cases, "the reliability of Horizon data was not essential to the prosecution case and that the convictions are safe".