A bench of justices A K Patnaik and V Gopala Gowda quashed the orders of the trial court and the Gujarat high court by which three persons were awarded death sentence.
Other convicts were facing varying jail terms from life sentence to 10 years for the September 24, 2002, terrorist attack on the Akshardham temple in Gandhinagar.
Adam Ajmeri, Shan Miya alias Chand Khan and Mufti Abdul Qyyum Mansuri were awarded the death sentence in July 2006 by the special court set up to try the case under POTA.
While a local youth from Dariapur in the city, Mohammed Salim Shaikh, was sentenced to life imprisonment, Abdulmiyan Qadri was given a 10-year term and Altaf Hussain was sentenced to imprisonment for five years.
The apex court ordered their release from jail "forthwith" if they are not convicted in any other case.
The bench pulled up the Gujarat police for shoddy its investigation in the case which concerned national security and integrity of the nation.
Two militants, identified as Murtuza Hafiz Yasin and Ashraf Ali Mohammed Farooq, and allegedly having links with the Pakistan-based Lashkar-e-Tayiba, were killed by National Security Guard commandos during the attack on the temple.
The attackers, using automatic weapons and hand grenades, had killed 32 people, including 28 temple visitors, three commandos, including one from the NSG, and a constable of the State Reserve Police.
The apex court held that sanction to prosecute the arrested persons under POTA was "void" on ground of non-application of mind and the sanction was illegal and invalid.
The bench said their confessional statements under POTA were not recorded by following appropriate procedure as sufficient cooling time, which was required under the law, was not granted.
It held that the prosecution story was conceived after "good deliberation" and the prosecution was unable to prove its case beyond reasonable doubt.
The court said the statement of the approver was not corroborated by other evidence.
Image: Anand Swami, one of the ascetics who briefed commandos on the layout of the Akshardham temple on Tuesday, played a critical role in saving several lives.
The Akshardham aftermath
Anti-terrorism lessons from Akshardham
'It bears the ISI's stamp'
The silent heroes of Akshardham
'New govt should instill confidence in Muslims while fighting terror'