Hamid Abd al-Rahman Ahmad was the first detainee to be transferred to Spain from the prison camp where the US is holding about 600 people captured since its forces invaded Afghanistan in 2001.
Judicial sources said investigating magistrate Baltasar Garzon had ordered Abd al-Rahman be released on a bail of $3700. He has to report daily to a police station and weekly to court and is forbidden from leaving the country.
Embracing those waiting for him outside the Alcala de Henares jail, near Madrid, the former Guantanamo detainee thanked the judge whose investigation into al-Qaida's Spanish activities got him out of the US detention centre in Cuba.
Garzon had charged him with belonging to al-Qaida and once the US handed him over to Spain in February, he ordered him jailed pending trial.
But the evidence against him has proven so weak that Garzon saw no reason not to allow bail while the investigation is completed.
"I am innocent. I am not a terrorist," Abd al-Rahman told reporters outside the jail. "I was just in the wrong place at the wrong time, that's all."
Originally from Spain's African enclave of Ceuta, he was arrested by the Pakistani army in November 2001.
In his first court appearance he said he had wanted to join the Taliban movement in Afghanistan but denied the charge of belonging to al-Qaida.
"The judge will certainly decide in a few days
that there are no charges to answer"
Marcos Garcia Montes,
Abd al-Rahman's lawyer
Lawyer Marcos Garcia Montes said the release was provisional but he expected his client to be cleared of all charges shortly.
"The judge will certainly decide in a few days that there are no charges to answer," Garcia Montes told journalists.
Spain has also asked for three more detainees to be sent to Spain for its investigations.
The Guantanamo detentions have brought criticism from human rights groups as none of the prisoners have been tried and the US calls detainees enemy combatants rather than prisoners of war, denying them rights set out in the Geneva Conventions.
On Monday, the US told Guantanamo prisoners they had the right to go to court to challenge their detention and ask for a hearing on their "enemy combatant" status.