The Lord Chancellor is to appeal against a High Court ruling blocking his decision to introduce a UK "residence test" for foreign-born individuals seeking legal aid in civil court cases.
Three judges in London unanimously ruled that residence was "not a lawful ground for discriminating between those who would otherwise be eligible for legal assistance".
The residence test was expected by human rights campaigners to have a particularly serious impact on recently-arrived migrants and their children.
A Ministry of Justice (MoJ) spokesman said civil legal aid was paid for by British taxpayers - "and we do not believe it is fair that people without a strong connection to the UK should be eligible when it is funded by taxpayers' hard-earned money."
Lord Justice Moses declared the Lord Chancellor had acted outside his powers. Mr Justice Collins and Mr Justice Jay unanimously agreed.
The test was introduced through secondary legislation in the form of a 2014 amendment to the Legal Aid, Sentencing and Punishment of Offenders Act 2012 (LASPO).
Government lawyers argued the country had one of the most expensive legal aid systems in the world and the move was necessary to save costs.
It was also necessary to restore confidence in the system following expressions of public concern over the expense.
Part 1 of Schedule 1 of LASPO identifies cases most in need of public funding for which the UK is not obliged to provide legal assistance under human rights legislation or the common law.
Lord Justice Moses said the real question in today's challenge was whether, once the United Kingdom chose to provide legal assistance in cases where it was under no duty to do so, it could refuse such assistance to non-resident claimants.
He ruled Schedule 1 of LASPO did not permit such discrimination "on the grounds that to do so might save costs".
And he added: "In the context of a discriminatory provision relating to legal assistance, invoking public confidence amounts to little more than reliance on public prejudice".
The judge said it was not difficult "to identify those on whom the application of the residence test would have a direct impact".
Coram Children's Legal Centre had cited the case of L, who recently came to the UK to be reunited with her refugee husband.
She would be unable to access legal advice in relation to the failure of the housing authority in the area she was living to assess the needs of her autistic eight-year-old son because she had only been in the UK for three months.
Nicola Mackintosh QC of Mackintosh Law told the court of P, an adult with severe learning disabilities "forced to live in a dog kennel" by his family and subjected to regular beatings by his brother and mother, and starved over an extensive period of time.
The judge said: "With the benefit of legal aid and the involvement of the Official Solicitor, proceedings in the Court of Protection resulted in a determination that it was in P's best interests to live separately from his family in a small group home with his friends and peers and 24-hour care."
If the residency test had been in place at the time, it would have been impossible to discover whether he met it and was entitled to legal assistance from the state.
Jo Hickman of the Public Law Project (PLP), which led the legal battle against the Lord Chancellor, said: "We are heartened by this judgment, which embodies and articulates the finest traditions of our justice system and provides a timely illustration of the importance of judicial review as a check on unlawful executive action."
John Halford of Bindmans, who acted for PLP, said today: "Using powers that were never his to exercise, the Lord Chancellor has attempted to refashion the legal aid scheme into an instrument of discrimination so that many of the cases Parliament itself identified as most worthy of support could never be taken.
"The court's judgment on that attempt is emphatic: it is simply unacceptable in a country where all are equal in the eyes of the law."
The MoJ spokesman said there would be an appeal, adding: "Legal aid is paid for by British taxpayers and we do not believe it is fair that people without a strong connection to the UK should be eligible for civil legal aid when it is funded by taxpayers' hard-earned money.
"The Government believes that in principle, people should have a strong connection to the UK in order to benefit from the civil legal aid scheme and that the residence test we proposed is a fair and appropriate way to demonstrate that connection.
"We listened very carefully to views as part of the consultation and made exemptions as a result - for example, people trying to flee war-torn countries."
Refugee Council policy manager Judith Dennis said: "This ruling is a triumph for justice and sends an important message to politicians: discriminating against people in order to save money is unacceptable and against the law."
The Children's Commissioner for England, Dr Maggie Atkinson, who outlined to the court the likely impact of the residence test, welcomed the ruling.
She said: "We decided to intervene in this case because we were seriously concerned about the effect of the residence test on vulnerable children and young people who could not access legal advice or support to secure their rights simply because they could not prove they had been in the UK for 12 months.
"This would include children fleeing domestic violence or abuse without access to their documents and children who, for reasons entirely beyond their control, had never had their immigration status regularised.
"Children cannot be expected to navigate the legal system without legal advice and assistance."