The Court of Appeal has ruled that the so-called bedroom tax discriminates against a domestic violence victim and the family of a disabled teenager.
The ruling followed legal challenges by a woman who has a panic room in her home, and the grandparents of a 15-year-old who requires overnight care.
The removal in 2013 of what the government calls the spare room subsidy cuts benefits for social housing tenants with a "spare" room.
Ministers have said they will appeal.
The Department for Work and Pensions (DWP) argued that it had given councils money to make discretionary payments to people facing hardship because of the policy change.
The case is now due to be decided in the Supreme Court.
Prime Minister David Cameron said the government would "look very carefully" at the judgement. "But our fundamental position is, it is unfair to subsidise spare rooms in the social sector if we don't subsidise them in the private sector."
One of the cases - brought by a woman identified as "A" - concerned the effect of the policy on women living in properties adapted because of risks to their lives. Her home was equipped with a panic room.
The second case - brought by Pembrokeshire couple Paul and Susan Rutherford and their 15-year-old grandson Warren - focused on the impact of the policy on disabled children needing overnight care.
The BBC's legal correspondent Clive Coleman said the ruling would affect only people within these two specific groups - severely disabled children needing overnight care and victims of domestic violence living in specially adapted accommodation.
There are believed to be about 300 such victims of domestic violence and thousands of severely disabled children in this situation.
The Court of Appeal ruling comes after a judicial review brought by the Rutherfords was dismissed in the High Court in 2014.
Housing benefit changes - dubbed the "bedroom tax" by Labour - were introduced in April 2013. Since then families claiming housing benefits have been assessed for the number of bedrooms they actually need.
Families deemed by their local authorities to have too much living space have received reduced benefits, with payments being cut by 14% if they have one spare bedroom.
Both "A" and the Rutherfords claimed that the policy change discriminated against them unlawfully.
Lord Chief Justice Lord Thomas, Lord Justice Tomlinson and Lord Justice Vos ruled in their favour, saying the "admitted discrimination" in each case "has not been justified by the Secretary of State".
Mr Rutherford said he was "absolutely delighted" with the ruling, adding: "I couldn't have had a better start to the day."
"It was so unfair that somebody had to do something to get the law changed."
Business minister Anna Soubry told BBC Radio 4 that she "very much hoped" the government would look again at this issue.
Michael Spencer, from the Child Poverty Action Group, said the ruling meant families "can stay in their homes safe in the knowledge that their disabled children can get the care they need".
Rebekah Carrier, a solicitor acting for "A", said: "Our client's life is at risk and she is terrified. The anxiety caused by the bedroom tax and the uncertainty about this case has been huge."
By Clive Coleman, BBC legal correspondent
Today's judgement is another example of the power of judicial review.
The spare room subsidy, more commonly known as the bedroom tax, could hardly be a more politically contentious policy.
The victories for the two families at the heart of these cases show once again that there is virtually no area of government decision making that cannot be scrutinised by the independent judiciary.
Judges are at pains not to make political decisions, but will step in where there is illegality.
A judicial review can be brought by anyone and allows the court to rule on whether the government (or any other public body) has acted lawfully.
Governments may find them frustrating, but they remain a critical part of our constitutional arrangements in holding public bodies to account.
A spokesman for the DWP said the government "fundamentally" disagreed with the court's ruling.
"We have already been granted permission to appeal to the Supreme Court. We know there will be people who need extra support.
"That is why we are giving local authorities over £870m in extra funding over the next five years to help ensure people in difficult situations like these don't lose out."
Shadow work and pensions secretary Owen Smith said the ruling provided "a glimmer of hope for the hundreds of thousands of people who have been hit by this cruel policy".
"Surely the time has now come for the Tories to discover a conscience, listen to the courts as well as the public, and scrap the hated bedroom tax."
The government - which rejects the term "bedroom tax" - says the regulations remove what is in fact a spare room subsidy.
It says it aims to encourage people to move to smaller properties, potentially saving about £480m a year from the housing benefit bill.