"To allow enemy combatancy to rest on so thin a reed would be inconsistent with the court's obligation," Leon told the crowded courtroom.
As a result, he said, "The court must and will grant the petitioners and order their release."
As for the sixth Algerian, Belkacem Bensayah, Leon said there was enough reason to believe he was close to an al-Qaida operative and had sought to help others travel to Afghanistan to join the terrorists' fight against the United States and its allies.
One of the men to be released is Lakhdar Boumediene, whose landmark Supreme Court case last summer gave the Guantanamo detainees the right to challenge their imprisonment.
The Algerians' attorneys said they would appeal Bensayah's detention but hugged each other and colleagues in congratulations after Leon's ruling.
"It's a relief," said attorney Robert C. Kirsch.
The Bosnian government already has agreed to take back the detainees, all of whom immigrated there from Algeria before they were captured in 2001.
The Justice Department had no immediate comment. Its lawyers huddled grimly in a corner of the courtroom after the hearing.
Leon also urged senior Justice Department leaders and high-level officials at other government agencies involved in the case not to appeal his ruling.
He said the five Algerians already have been improperly held for seven years, and deserve to go home. An appeal could delay their release for up to another two years, Leon said.
"This is a unique case," Leon said, trying to assuage any Justice Department fears that hundreds of other detainees also could be released based on Thursday's ruling. "Few if any others will be factually like it. Nobody should be lulled into a false sense that all of the ... cases will look like this one."
Leon was appointed by President Bush and has been sympathetic to the argument that the president has broad authority during wartime. In 2005, Leon ruled that this same group of detainees had no right to challenge their detention in civilian courts.
Thursday's ruling is the first since the Supreme Court cleared the way last June for civilian courts to hear challenges by detainees being held indefinitely without charges.
It largely hinged on Leon's definition of an enemy combatant, which he said included al-Qaida or Taliban supporters who directly assisted in hostile acts against the U.S. or its allies.
Much of the evidence against the Algerians is classified and could not be discussed during the few open court hearings in the two-week trial - or even with the detainees themselves. The detainees listened to Thursday's ruling through a translated telephone conference call, but could not be heard during the nearly one-hour hearing.
The government initially detained Boumediene and the other Algerians on suspicion of plotting to bomb the U.S. Embassy in Sarajevo in October 2001. They were transferred to Guantanamo in January 2002.
The Justice Department last month backed off the embassy bombing accusations, but said the six men were caught and detained before they could join terrorists' global jihad. The Justice Department said it needed to be proactive against threats, especially in the months after the Sept. 11, 2001 attacks on New York and Washington.
The detainee's lawyers denied the men ever planned to join the battlefield. Even if they had, the lawyers argued, they did not fit Leon's definition of an enemy combatant because they never joined the terrorist fighters.
The cases of more than 200 additional Guantanamo detainees are still pending, many in front of other judges in Washington's federal courthouse.