The weekly uplift was paid to universal credit claimants between March 2020 and October 2021, but people on other benefits were not given the extra cash.
Four claimants excluded from the uplift went to court, but a judge ruled the different treatment was justified.
Lawyers for the claimants are considering an appeal.
About two million people are on so-called "legacy benefits" such as jobseekers' allowance, income support and employment support allowance - these claimants were not eligible for the £20 weekly uplift sanctioned by the government to support people during the pandemic.
Universal credit is a single benefit payment for working-age people, which was introduced to replace a range of different benefits for unemployed and low-paid people, and make the system simpler.
It's claimed by more than 5.8 million people in England, Scotland and Wales, both in and out of work - 40% of universal credit claimants are workers.
Claimants received an extra £20 per week during the pandemic - from March 2020 to October 2021 - when many people lost their jobs unexpectedly and had to cope with reduced incomes.
Lawyers for the four people who brought the case for a judicial review argued that those on legacy benefits faced the same financial pressures as those on universal credit, and the government's decision not to treat them the same amounted to unlawful discrimination.
In his judgement, Mr Justice Swift highlighted the government's objective of helping people who had lost their livelihoods during the pandemic, as claims for universal credit doubled to 5.3 million between March and May 2020.
"The central question raised by the Claimants' discrimination claims is whether it was lawful for the Secretary of State to direct attention to the position of new benefits claimants, all of whom would have made claims for universal credit.
"I consider that she was. New benefits claimants would need to adjust to a loss in income. They would be affected differently to persons already claiming benefits."
William Ford, partner at Osbornes Law, who represent the four individuals who brought the case against the government said they were "extremely disappointed by today's judgment" and would look at any possible grounds to appeal.
"The court's decision is a devastating blow to more than two million people who we consider were unjustly deprived of the £20 uplift given to those who receive universal credit during the pandemic.
"It is deeply unfair that those on so-called legacy benefits should be discriminated against in this way, and we will look to see if we can continue to fight the government on this issue to get our clients and everybody else on legacy benefits justice."
One of the four claimants, Phil Wayland, from Rochford in Essex, has relied on employment support allowance for 11 years.
He has been diagnosed with anxiety and depression, and his condition means he cannot work in his previous role as a floor-layer and carpet-fitter.
He says surviving on his benefit payments became harder during the pandemic. He lives with his mother, a pensioner, and says if it wasn't for that "I'd be in queues for foodbanks".
Mr Wayland believes people on employment support allowance, who are unable to work because of ill health, are the poorest and most vulnerable in society, Not allowing them the additional weekly £20 was, he says, "just blatantly discriminatory. I felt compelled to try and do something really".
Legacy benefits are gradually being phased out and replaced by universal credit, but this process is expected to take until at least 2024.