A Hamburg court has released Abdelghani Mzoudi - only the second person in the world to stand trial over the 11 September 2001 attacks on the US.
Mr Mzoudi was baffled by the announcement
Judge Klaus Ruehle said he had taken the decision after receiving new evidence that could exonerate him.
The court will sit next Thursday to hear prosecution complaints about the reliability of the evidence.
After the ruling, lawyers for the only man jailed on 11 September-related charges demanded his release, too.
Arrest warrant lifted
The 31-year-old Moroccan was originally charged with being an accessory to the murders of more than 3,000 people who died in the attacks in New York and Washington. He was alleged to have belonged to the Hamburg al-Qaeda cell responsible for the attacks.
The new testimony was submitted by Germany's federal criminal investigation bureau (BKA), but it did not name the source.
The witness is reported to have testified that the Hamburg cell consisted of only four people.
Three of the members were suicide pilots in the attacks on New York and Washington, and the fourth - Ramzi Binalshibh - is an alleged senior al-Qaeda operative currently in US detention.
Judge Ruehle told the court that the arrest warrant for Mr Mzoudi had been lifted as there was no further reason for his detention.
"There is the serious possibility that Mzoudi was
purposefully left out of the attack plans despite his links to the Hamburg group and despite his stay in Afghanistan and that his supportive actions were not consciously made," the judge was quoted as saying by Reuters news agency.
Mr Mzoudi's lawyers have argued that he simply helped fellow Muslims and had no idea about a hijack plot.
He appeared baffled by the judge's surprise ruling - and broke into a smile when one of the lawyers slapped him on the shoulder.
"Of course he was hugely pleased, but he couldn't really believe it," the lawyer said.
But there is some contention over the identity of the source of the new evidence.
Last month, the court rejected an application from the defence to hear evidence from Ramzi Binalshibh.
State prosecutor Walter Hemberger, who opposed the request for Mr Mzoudi's release, argued that the new evidence could only have come from Mr Binalshibh - and the judge agreed.
Mr Hemberger said Mr Binalshibh's word could not be trusted, and was likely to be motivated by a desire to protect others and cover up details of the plot.
Mr Mzoudi left the court in a taxi with his lawyers to return next week when the trial is set to continue to hear more on the source.
Meanwhile, a lawyer for Moroccan Mounir al-Motassadek, convicted and jailed in Germany on identical charges, has said he will demand his immediate release.
"We will make an immediate move to get him out - this is clear," Josef Graessle-Muenscher told Reuters news agency.
"If Ramzi Binalshibh said the cell was only four people, and this was him and the three hijackers then the case is clear."
Motassadek was sentenced to 15 years in jail last February for plotting with the 11 September hijackers.
The collapse of Mr Mzoudi's prosecution, and the doubts now hanging over the conviction of Motassadek on similar charges, show how hard it is proving to bring anyone to justice for the 11 September attacks, says the BBC's Elizabeth Blunt.
Mr Mzoudi, she says, shared a flat in Hamburg with Mohamed Atta, believed to be the ringleader in the attacks.
He went to the same mosque, attended the same Islamic discussion group, and witnessed Mohamed Atta's will. He was certainly a friend of some of the hijackers and did things to help them, she says.
But the prosecution has not managed to prove that he knew about their plans; indeed, the alleged attackers themselves might not have been recruited until later than had been thought, she says.