The mosque was built by the Mughal emperor Aurangzeb in 1669 CE, after destroying a Hindu temple. The remnants of the Hindu temple can be seen on the walls of the Gyanvapi mosque. The demolished temple is believed by Hindus to be an earlier restoration of the original Kashi Vishwanath temple. The original temple had been destroyed and rebuilt a number of times. The temple structure that existed prior to the construction of the mosque was most probably built by Raja Man Singh during Akbar's reign.
Aurangzeb's demolition of the temple was motivated by the rebellion of local zamindars (landowners), some of who may have facilitated the escape of the Maratha king Shivaji. Jai Singh I, the grandson of the temple's builder Raja Man Singh, was widely believed to have facilitated Shivaji's escape from Agra. In addition, there were allegations of Brahmins interfering with the Islamic teaching. The temple's demolition was intended as a warning to the anti-Mughal factions and Hindu religious leaders in the city.
Maulana Abdus Salam Nomani (d. 1987), an Imam of the Gyanvapi mosque, contested the claim that a temple was destroyed to build the mosque. According to him, the foundation of the mosque was laid by the third Mughal emperor Akbar, and Akbar's grandson and Aurangzeb's father Shah Jahan started a madrasah called Imam-e-Sharifat at the site of the mosque in 1048 hijri (1638-39 CE).
The mosque is named after a well, the Gyan Vapi ("the well of knowledge"), which is located within the mosque precincts. The legends mentioned by the Hindu priests state that the lingam of the original temple was hidden in this well, when the temple was destroyed.
During the British period, the Gyan Vapi well was a regular destination on the Hindu pilgrimage routes in the city. Reginald Heber, who visited the site in 1824, mentioned that the water of the Gyan Vapi — brought by a subterraneous channel of the Ganges — was considered holier than the Ganges itself by the Hindus. M. A. Sherring, in his 1868 book The Sacred City of the Hindus, mentioned that people visited the Gyan Vapi "in multitudes", and threw in offerings that had polluted the well. Greaves (1909) mentioned that a Brahmin (Hindu priest) sat at a stone screen surrounding the Gyan Vapi. The worshippers would come to the well, and receive sacred water from the priest.
During the Hindu-Muslim riot of 1809, a Muslim mob killed a cow (sacred to Hindus) on the spot, and spread its blood into the sacred water of the well. In retaliation, the Hindus threw rashers of bacon (haram to Muslims) into windows of several mosques. Subsequently, both the parties took to arms, resulting in several deaths, before the British administration quelled the riot.