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.”