WASHINGTON  A federal judge ruled on Thursday that some prisoners held by the United States military in Afghanistan have a right to challenge their imprisonment, dealing a blow to government efforts to detain terrorism suspects for extended periods without court oversight.

In a 53-page ruling that rejected a claim of unfettered executive power advanced by both the Bush and Obama administrations, United States District Judge John D. Bates said that three detainees at the United States’ Bagram Air Base had the same legal rights that the Supreme Court last year granted to prisoners held at the American naval base in Guantánamo Bay, Cuba.

The three detainees  two Yemenis and a Tunisian  say that they were captured outside Afghanistan and taken to Bagram, and that they have been imprisoned for more than six years without trials. Arguing that they were not enemy combatants, the detainees want a civilian judge to review the evidence against them and order their release, under the constitutional right of habeas corpus.

The importance of Bagram as a holding site for terrorism suspects captured outside Afghanistan and Iraq has increased under the Obama administration, which prohibited the Central Intelligence Agency from using its secret prisons for long-term detention and ordered the military prison at Guantánamo closed within a year. The administration had sought to preserve Bagram as a haven where it could detain terrorism suspects beyond the reach of American courts, telling Judge Bates in February that it agreed with the Bush administration’s view that courts had no jurisdiction over detainees there.