Government policies on discharging untested patients from hospital to care homes in England at the start of the Covid pandemic have been ruled unlawful by the High Court.
The ruling came after two women took Public Health England and the health secretary, then Matt Hancock, to court.
Dr Cathy Gardner and Fay Harris said it had caused a "shocking death toll".
Prime Minister Boris Johnson renewed his apologies for all those who lost loved ones during the pandemic.
When Covid hit in early 2020, patients were rapidly discharged into care homes without testing, despite the risk of asymptomatic transmission, with government documents showing there was no requirement for this until mid-April.
Dr Gardner and Ms Harris said there had been failures in protecting residents and brought the legal challenge.
They partially succeeded in claims against Public Health England and the secretary of state for health and social care.
Speaking outside court, Dr Gardner said she "believed all along that my father and other residents of care homes were neglected and let down by the government".
"The High Court has now vindicated that belief, and our campaign to expose the truth," she said.
In their judgement, Lord Justice Bean and Mr Justice Garnham found the decisions of the then health secretary to make and maintain a series of policies contained in documents issued on 17 and 19 March and 2 April 2020 were unlawful.
They ruled this was on the grounds the drafters of those documents failed to take into account the risk to elderly and vulnerable residents from non-symptomatic transmission, which had been highlighted by Sir Patrick Vallance in a radio interview as early as 13 March.
Sir Patrick told the Today programme that "it's quite likely that there is some degree of asymptomatic transmission".
A barrister representing Dr Gardner, from Sidmouth in Devon, and Ms Harris, from Medstead in Hampshire, told the court at a hearing in March that more than 20,000 elderly or disabled care home residents died from Covid between March and June 2020 in England and Wales.
Jason Coppel QC also said in a written case outline for the judicial review that the care home population was known to be "uniquely vulnerable" to Covid.
"The government's failure to protect it, and positive steps taken by the government which introduced Covid-19 infection into care homes, represent one of the most egregious and devastating policy failures in the modern era," he added.
By Alison Holt, BBC Social Affairs Editor
For many of the families whose loved ones died in care homes in the first difficult, traumatic months of the pandemic this judgment is highly significant.
The government has repeatedly said that it put a "protective ring" around care homes in England from the very start, but to nearly all the families and care staff I have spoken to over the last two years it felt very different.
Repeatedly they have said they felt abandoned or forgotten by the government in those early days.
As well as the rapid discharge of hospital patients into care homes, they point to guidance which they felt was slow to arrive and the difficulties getting protective equipment.
There is no doubt that decisions early in the pandemic had to be made in the most difficult, rapidly changing circumstances.
Policies and support for the care sector also improved as time went on.
But the hope will be that if or when there is a next time, there will be a much greater understanding of the importance of protecting the many older and disabled people living in care homes, as well as the NHS.
Dr Gardner's father Michael Gibson was 88 when he died on 3 April 2020 while living in a home in Oxfordshire during the UK's first lockdown.
His cause of death was given as "suspected Covid" after the home took in a patient discharged from a hospital with the virus.
Former Royal Marine Donald Harris, 89, died in Alton, Hampshire, on 1 May 2020 after an outbreak in his care home.
His daughter, Ms Harris, said she was "very pleased" with the judgment and it brought "some comfort".
She said the government's actions "exposed many vulnerable people to a greater risk of death - and many thousands did die".
"It has only increased the distress to me and many others that the government have not been honest and owned up to their mistakes," she added.
Both women also said Mr Johnson should resign in the wake of the ruling.
In their ruling the judges said: "This was not a binary question - a choice between on the one hand doing nothing at all, and on the other hand requiring all newly admitted residents to be quarantined.
"The document could, for example, have said that where an asymptomatic patient, other than one who has tested negative, is admitted to a care home, he or she should, so far as practicable, be kept apart from other residents for up to 14 days.
"Since there is no evidence that this question was considered by the secretary of state, or that he was asked to consider it, it is not an example of a political judgment on a finely balanced issue.
"Nor is it a point on which any of the expert committees had advised that no guidance was required.
"Those drafting the March Discharge Policy and the April Admissions Guidance simply failed to take into account the highly relevant consideration of the risk to elderly and vulnerable residents from asymptomatic transmission."
The judges said these issues were not addressed until a further document was issued in mid-April 2020.
They rejected other claims made under human rights legislation, and against NHS England.
A spokesman for Mr Hancock said the case "comprehensively clears ministers of any wrongdoing and finds Mr Hancock acted reasonably on all counts".
"The court also found that Public Health England failed to tell ministers what they knew about asymptomatic transmission," she said.
"Mr Hancock has frequently stated how he wished this had been brought to his attention earlier."
- On 19 March 2020, NHS guidance said "unless required to be in hospital, patients must not remain in an NHS bed"
- On 2 April, the rules on discharging to care homes were clarified, saying "negative [coronavirus] tests are not required prior to transfers/admissions into the care home"
- From 15 April, the government said all patients discharged from hospitals would be tested for coronavirus
During Prime Minister's Questions in the House of Commons earlier, Mr Johnson said: "Of course I want to renew my apologies and sympathies for all those who lost loved ones during the pandemic, people who lost loved ones in care homes.
"I want to remind the house of what an incredibly difficult time that was and how difficult that decision was. We didn't know very much about the disease.
"The thing that we didn't know in particular... was that Covid could be transmitted asymptomatically in the way that it was. And that was something that I wish we had known more about at the time."
A government spokesperson said they "specifically sought to safeguard care home residents based on the best information at the time".
They added "the vast majority of the judgement found in the government's favour", but the government acknowledged the judges' comments "on assessing the risks of asymptomatic transmission and our guidance on isolation, and [it] will respond in more detail in due course".
Labour's shadow health secretary Wes Streeting claimed "ministers ignored" warnings and "cannot claim to have acted to save lives".
"They broke the law and people died," he said.
"We owe it to bereaved families to make sure that this never happens again."
Helen Wildbore, director of the Relatives and Residents Association, a charity representing older people in care and their families, said older people were "abandoned" at the start of the pandemic.
She said: "The ruling is very welcome as a first step to justice, but bereaved families will be left asking why more wasn't done to protect their loved ones and how many lives could have been saved."
Charlie Williams, whose father died in a care home in April 2020, was told that nobody in the home had coronavirus.
However, he later found out that 27 residents had died from the virus.
Mr Williams, a spokesman for the Covid-19 Bereaved Families for Justice group, said: "We now need to see those responsible for those dark days held accountable and lessons learned to save lives, ensuring the grim scenes of spring 2020 are never repeated again."
A public inquiry to "learn lessons" from the pandemic for the future is due to be begin next year.
Its mandate includes looking at how well prepared the UK was for a pandemic and how the NHS has coped with it.