A new points-based system was introduced in 2006
The government acted unlawfully in changing immigration rules for highly skilled workers who want to stay in the UK, the High Court has ruled.
In 2006, a new "points" system, based on education, previous salary and age, was introduced, changing the criteria for remaining in the country.
Opponents say 44,000 people in the UK under old rules must leave - but the Home Office says 1,370 are affected.
Judge Sir George Newman ruled that the original scheme should be honoured.
The government is considering an appeal against the ruling.
Under the Highly Skilled Migrants Programme, introduced in 2002, non-EU workers such as doctors, engineers and financiers were originally given UK entry for a year.
They could apply for a two-year extension, then a further three years before applying for settlement.
This was judged according to qualifications, experience and earning ability.
But in November 2006, the home secretary changed the rules, so anyone applying to extend their work visa would have to score points based on their education, salary and age.
The Highly Skilled Migrants Programme Forum group brought the case against the government, arguing the new system was "grossly unfair" and "a clear case of breach of legitimate expectation".
The group claims 90% of those who arrived before 2006, about 44,000 people, would no longer qualify to remain in the UK and accused the government of going back on a promise to let them stay after several years' work.
But the government says regulating the number of immigrants to the UK is in the country's interest.
A Home Office spokeswoman said that the number of people affected by the change in the rules was much lower than claimed.
She said that about 16,000 people who arrived under the old rules would have to meet the new points criteria.
The Home Office estimates that about 1,370 of them would no longer qualify. The court ruling means they should be continue to be judged under the old scheme.
Liberal Democrat home affairs spokesman Chris Huhne said: "Today's ruling makes it clear that the government's decision to change the rules for highly skilled immigrants already working in this country was not only deeply unfair but also completely illegal."
He added: "Changes like these must never be made retrospectively. Ministers must not only accept this ruling, but also undertake never again to introduce changes in the rules without properly checking that they are fair, proportionate and legal."