The gun battle at the disputed site in Ayodhya town, about 600km southeast of New Delhi, had ended and the complex was in the control of police, TV reports said on Tuesday.
"Five militants have been killed. Fortunately all the pilgrims are safe and the site is protected ... three [members of the] security forces have been injured," said Alok Sinha, the home secretary of Uttar Pradesh state, where the temple complex is located.
The complex houses a makeshift temple to Hindu deity Ram, erected after Hindu mobs destroyed a 16th century mosque there in 1992, saying it was built on the site where they believe Ram was born thousands of years ago.
Sinha said the attackers used two vehicles in the assault - a jeep loaded with bombs that blew up part of a wall at the outer periphery of the high-security complex, and a car which they drove through the breach in the wall.
Sinha said it was too early to say who was involved.
"There has been a security lapse. We will fix responsibility and take corrective action," said Arun Kumar Sinha, a senior local official.
Ayodhya is guarded at all times by thousands of police and paramilitary soldiers, and the site has multiple barricades where every worshipper is frisked before being allowed in.
Security is so tight that even pens, pencils, lighters and matchboxes are prohibited.
The disputed site at Ayodhya created deep divisions between India's Hindus and its minority Muslims after the nationalist Bharatiya Janata Party (BJP) led a campaign in the late 1980s to build a Ram temple on the site of the mosque, culminating in its destruction.
Reports of Tuesday's attack hurt Indian shares, which fell as far as to 7249.55 points after hitting a record high of 7308.72 in early trade. The main Bombay index later recovered slightly and was down 0.14% at 7267.88 points at 0115 GMT.
Even before the shootout with police was over, Hindu nationalists blamed Pakistan-backed "jihad terrorists" for the attack and said the incident proved India's recent peace overtures to Islamabad were a failure.
The main opposition BJP called for a nationwide strike on Wednesday to protest the assault.
"To attack the Ram Janbhoomi, the holiest shrine of the Hindus, is a very serious thing and there should be an equal reaction"
BJP president Lal Krishna Advani
"To attack the Ram Janbhoomi, the holiest shrine of the Hindus, is a very serious thing and there should be an equal reaction," BJP president Lal Krishna Advani said.
But the parent body of BJP appealed to the public to keep any protests peaceful. A leading Islamic leader also urged peace, saying the attackers helped neither their faith nor their country.
Meanwhile, Indian Prime Minister Manmohan Singh called a cabinet meeting in New Delhi to discuss the possible fallout of the attack.