Pakistan's powerful security agency, Inter-Services Intelligence (ISI), has admitted to holding seven suspected militants for more than a year without sufficient evidence to try them.
Raja Irshad, a lawyer for the ISI, told Pakistan's Supreme Court that officials kept the men in detention because they were convinced about their involvement in terrorism.
The ISI arrested the seven as part of a group of 11 suspects in connection with a 2007 suicide bombing against intelligence personnel and a rocket attack on an air force base.
Four among them died in mysterious circumstances in custody.
An antiterrorism court ordered them to be freed in May 2010 but they were further detained.
The case is widely seen as a test for the Supreme Court's efforts to make the country's shadowy intelligence agencies accountable.
Raja Irshad, a lawyer representing the ISI and military intelligence, sought to justify the men's detention but conceded that they could not be put on trial because of a "lack of incriminating evidence".
"But we are morally convinced that they were involved in terrorism," Irshad told the court.
Chief Justice Iftikhar Muhammad Chaudhry said suspects cannot be detained indefinitely and unlawfully.
"Morally they can put any one behind bars, even me. According to them, all the people are guilty," Chaudhry said.
"They should have been released if they could not be tried under the army act. They are in confinement for more than four years," he added.
The case originally concerned a group of 11 men, but the court was told last year that four of them had died.
Pakistan has been suffering from a Taliban-led domestic insurgency since 2007, and Islamist militant bombings have killed thousands since the 9/11 attacks on the United States, which sparked the war in neighbouring Afghanistan.
Judge Sheikh Azmat Saeed expressed dismay that the government had not enacted appropriate legislation to deal with terror cases.
"They have not made the necessary legislation. Don't they see what kind of laws are in other countries, they should look at the Internet," Saeed said.
"What has the government been doing for the past five years?"
Chaudhry ordered the government and intelligence agencies to report back to him on Tuesday about what would happen to the detainees.