But the appeals court reversed his ruling in a unanimous decision.
Theatre of war
It drew a distinction between the situation at Bagram and at the US military prison at Guantanamo, which still holds about 180 foreign terrorism suspects and which the Obama administration has pledged to close.
"While it is true that the United States holds a leasehold interest in Bagram, and held a leasehold interest in Guantanamo, the surrounding circumstances are hardly the same," the appeals court said in a 26-page opinion written by Chief Judge David Sentelle.
The United States has maintained total control over the Guantanamo base for more than 100 years, but there is no indication the US intends to permanently occupy the Bagram base, he said.
Sentelle also said Bagram remains in a theatre of war, unlike Guantanamo, and is not under US sovereignty.
"The United States holds the detainees pursuant to a co-operative arrangement with Afghanistan on territory as to which Afghanistan is sovereign," he wrote.
There are more than 800 prisoners held at the Bagram base.
In New York, Melissa Goodman of the American Civil Liberties Union denounced the ruling, saying it "ratifies the dangerous principle that the US government has unchecked power to capture people anywhere in the world, unilaterally declare them enemy combatants and subject them to indefinite military detention with no judicial review".
|The prison at the US air force base in Bagram houses hundreds of detainees [AFP]
Opposition Republican members of congress, however, such as Senator Lindsey Graham, praised the decision.
"There is a reason we have never allowed enemy prisoners detained overseas in an active war zone to sue in federal court for their release. It simply makes no sense and would be the ultimate act of turning the war into a crime," he said.
The ruling involved three detainees from countries other than Afghanistan - Fadi al-Maqaleh and Amin al-Bakri from Yemen and Redha al-Najar from Tunisia.
They say they were taken into custody in 2002 or 2003.
The US Supreme Court ruled in 2008 that the Guantanamo prisoners can challenge their confinement in US courts, but it has refused to allow similar lawsuits by those who were held by the US military in Iraq.