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 death sentence in July 2006 by the special court set up to try the case under Prevention of Terrorism Act (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 Pakistan-based Lashkar-e-Taiba, were killed by National Security Guard (NSG) 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 NSG, and a constable of the State Reserve Police (SRP).