"Bagram detainees who are not Afghan citizens, who were not captured in Afghanistan and who have been held for an unreasonable amount of time" may invoke the right to trial, John Bates, a US district judge, ruled in Washington on Thursday.
The US supreme court has previously ruled that detainees held at the US prison camp at Guantanamo Bay had the right to challenge their detention, and Bates argued that the Bagram detainees faced the same situation.
Bates said his ruling only applied to the part of the legal action specifically seeking the release of the Bagram detainees and did not affect their legal challenge regarding conditions at the base.
Afghan held
The case centred on two Yemenis, a Tunisian and also an Afghan national who have all been held at Bagram for at least six years.
Bates said the three foreign detainees had the legal right to challenge their detention but postponed a decision on Haji Wazir, the Afghan national.
Releasing him could create "practical obstacles in the form of friction with the host country," Bates wrote.
He ordered Wazir and the government to file memos addressing those issues.
Obama has ordered the Guantanamo prison, which has been widely criticised by human rights groups, to be closed within one year.