Guidance making it harder for doctors from overseas to take up training posts in the NHS has been ruled unlawful.
The Department of Health said they could only be considered for a training post if there was no suitable graduate from the UK or European Union.
The Court of Appeal has ruled that they must be allowed to compete for training posts on an equal footing.
The Department of Health warned that home grown graduates would now face stiffer competition for training posts.
Thousands of doctors came to the UK under the Highly Skilled Migrant Programme.
They were initially promised permanent residence after four years. However, this status was changed retrospectively.
This meant that they stood to fall foul of the Department of Health guidance, which effectively put doctors who did not have indefinite leave to remain in the UK at the back of the queue for training posts.
The guidance was challenged by the British Association of Physicians of Indian Origin (BAPIO), which argued it was unfair to its members.
Mr Ram Moorthy, chairman of the BMA's Junior Doctors Committee, welcomed the decision.
He said: "Doctors on the Highly Skilled Migrant Programme came to the UK in the honest expectation of careers in the NHS.
"The BMA has never wavered from the view that they should be able to compete for training posts alongside their UK colleagues.
"What is crucial now is that the government produces clear guidance for international medical graduates applying for jobs next year.
"The chaotic implementation of the new immigration rules left overseas doctors exposed to potential discrimination."
Anthony Robinson, a solicitor at Linder Myers, which represented the BAPIO, said the NHS had for many years relied upon overseas doctors - particularly from the Indian sub-continent - to cover the shortage of home grown doctors.
He said: "Now that more UK graduates are coming through the Department of Health has been trying to get round the rights of HSMP doctors who have already made the UK their home because it failed to plan ahead.
"This judgment is welcomed by several thousand doctors working in the NHS whose careers have been under threat."
A Department of Health spokesperson said the ruling was "disappointing".
"It means we no longer have the option of issuing the guidance for 2008 on which we were consulting that prioritises UK medical school graduates for specialty training posts.
"We face the prospect of a large number of applicants competing for places.
"Doctors from outside Europe have made and continue to make a huge contribution to the NHS.
"The issue is not, and never has been, whether they can continue to work as NHS doctors - which they can - but whether the taxpayer should be investing in training them instead of UK medical graduates."
The BAPIO had also appealed against a High Court ruling that the government did not have a legal duty to consult overseas doctors before tightening immigration rules last year.
This appeal was unsuccessful. ( BBC )