About 70 "Black Cat" commandos moved in overnight to end the 12-hour siege at the Swaminarayan Temple in the state capital, Gandhinagar.
One commando was also killed in the ensuing gun battle and others were reported injured.
The Indian Deputy Prime Minister, Lal Krishnan Advani, who went to the scene, indirectly blamed Pakistan for the attack - but Islamabad has condemned the killings and denied any involvement.
Indian leaders have appealed to the people of Gujarat to remain calm and the army has announced it is sending in 3,000 troops.
Hundreds of people died in Gujarat in February and March this year in India's worst religious bloodshed in a decade.
'Planned'
"The two attackers were killed shortly after daybreak," said Brigadier Raj Sitapathy, who led the assault.
He said the "bloodbath" - the victims included 13 men, six women and four children - had occurred in a prayer hall.
The two attackers were carrying letters pouring "venom" on India, Brigadier Sitapathy told the BBC before suggesting they belonged to a Pakistani-based militant group called Tehrik-e-Kasak - a front, he said, for the outlawed Lashkar-e-Toiba.
But he said it was too early to say they were Pakistani nationals.
Temple facts:
"Our enemy has been speaking of Gujarat in recent times and even last week our enemy spoke of Gujarat in the United Nations, so it seems that the plan was on for quite some time," Mr Advani told reporters at the site of the attack.
He was referring to Pakistani President General Pervez Musharraf's speech at the UN, where he called for "accountability" for the violence that has rocked Gujarat this year.
But a Pakistani government minister has blamed the Indian Government for failing to build a tolerant society in Gujarat.
Indiscriminate shooting
Eyewitnesses told of the bloodshed inside the temple complex as the men opened fire with assault rifles.
"We were sitting under a tree when they started firing indiscriminately. People started running here and there," said Balwant Shukwant Patel, 25.
One volunteer, his clothes stained with blood, said he had brought out about 20 casualties, although he was not sure how many of them were dead.
Policemen and priests were among the wounded.
Gujarat witnessed widespread religious violence earlier this year when Muslim mobs were accused of setting fire to a train carrying Hindu activists.
More than 1,000 people - mainly Muslims - were killed in the ensuing rioting. Some estimates put the figure as high as 2,000.
The Swaminarayan organisation, part of the Hindu faith, was formally established in 1907. It preaches religious tolerance and practical spirituality.
Around two million people visit the temple in Gandhinagar every year where the complex is spread over 23 acres (10 hectares).