The shooting came after the militants were surrounded in a residential building in an operation against “especially dangerous criminals”.
Police said one of the dead militants was wearing a suicide belt.
Kazan is capital of a region with large numbers of Muslim Tatars and Bashkirs.
Officials said the militants had been planning an attack to coincide with the Kurban Bayram (Eid al-Adha) holiday, to be celebrated on Thursday (today) and Friday.
Special security operations against Islamists are relatively common in the volatile North Caucasus region but rare in Tatarstan, the BBC reports.
Officials said the gunmen had been holed up on the ground floor of an apartment building in the suburbs of Kazan.
Attempts were made to arrest them but they detonated an explosive device, the officials added.
The two militants died as security forces stormed the building. It was not clear whether any others survived.
Two members of the Federal Security Service were badly injured in the raid, and one later died of his wounds.
Some reports said the militants were involved in the attempted assassination in July of Tatarstan mufti Ildus Fayzov and the near-simultaneous killing of a former deputy mufti of the region, Valiulla Yakupov.