The Sikh religion began around the time of the conquest of Northern India by Babur, the founder of the Mughal Empire. His conquering grandson, Akbar the Great, supported religious freedom and after visiting the langar of Guru Amar Das got a favourable impression of Sikhism. As a result of his visit he donated land to the langar and the Mughals did not have any conflict with Sikh gurus until his death in 1605. His successor Jahangir, however, saw the Sikhs as a political threat. He ordered Guru Arjun Dev, who had been arrested for supporting the rebellious Khusrau Mirza, to change the passage about Islam in the Adi Granth. When the Guru refused, Jahangir ordered him to be put to death by torture. Guru Arjan Dev's martyrdom led to the sixth Guru, Guru Hargobind, declaring Sikh sovereignty in the creation of the Akal Takht and the establishment of a fort to defend Amritsar. Jahangir attempted to assert authority over the Sikhs by jailing Guru Hargobind at Gwalior, but released him after a number of years when he no longer felt threatened. The Sikh community did not have any further issues with the Mughal empire until the death of Jahangir in 1627. The succeeding son of Jahangir, Shah Jahan, took offence at Guru Hargobind's "sovereignty" and after a series of assaults on Amritsar forced the Sikhs to retreat to the Sivalik Hills.
The next guru, Guru Har Rai, maintained the guruship in these hills by defeating local attempts to seize Sikh land and playing a neutral role in the power struggle between two of the sons of Shah Jahan, Aurangzeb and Dara Shikoh, for control of the Mughal Empire. The ninth Guru, Guru Tegh Bahadur, moved the Sikh community to Anandpur and travelled extensively to visit and preach in defiance of Aurangzeb, who attempted to install Ram Rai as new guru. Guru Tegh Bahadur aided Kashmiri Pandits in avoiding conversion to Islam and was arrested by Aurangzeb. When offered a choice between conversion to Islam and death, he chose to die rather than compromise his principles and was executed.
Guru Gobind Singh assumed the guruship in 1675 and to avoid battles with Sivalik Hill rajas moved the guruship to Paunta. There he built a large fort to protect the city and garrisoned an army to protect it. The growing power of the Sikh community alarmed the Sivalik Hill rajas who attempted to attack the city but Guru Gobind Singh's forces routed them at the Battle of Bhangani. He moved on to Anandpur and established the Khalsa, a collective army of baptised Sikhs, on 30 March 1699. The establishment of the Khalsa united the Sikh community against various Mughal-backed claimants to the guruship. In 1701, a combined army of the Sivalik Hill rajas and the Mughals under Wazir Khan attacked Anandpur. The Khalsa retreated but regrouped to defeat the Mughals at the Battle of Muktsar. In 1707, Guru Gobind Singh accepted an invitation by Aurangzeb's successor Bahadur Shah I to meet him. The meeting took place at Agra on 23 July 1707.
In August 1708 Guru Gobind Singh visited Nanded, the seat of Mughal Telangana Subah. There he met a Bairāgī recluse, Madho Das, and converted him to Sikhism, giving him a new name, Banda Singh.
Banda Singh Bahadur (1670–1716; also known as Lachman Das, Lachman Dev and Madho Das) met Guru Gobind Singh at Nanded and adopted the Sikh religion. A short time before his death, Guru Gobind Singh ordered him to reconquer Punjab region and gave him a letter that commanded all Sikhs to join him. After two years of gaining supporters, Banda Singh Bahadur initiated an agrarian uprising by breaking up the large estates of Zamindar families and distributing the land to the poor peasants who farmed the land. Banda Singh Bahadur started his rebellion with the defeat of Mughal armies at Samana and Sadhaura and the rebellion culminated in the defeat of Sirhind. During the rebellion, Banda Singh Bahadur made a point of destroying the cities in which Mughals had been cruel to the supporters of Guru Gobind Singh. He executed Wazir Khan in revenge for the deaths of Guru Gobind Singh's sons and Pir Budhu Shah after the Sikh victory at Sirhind. He ruled the territory between the Sutlej river and the Yamuna river, established a capital in the Himalayas at Lohgarh and struck coinage in the names of Guru Nanak and Guru Gobind Singh. In 1716, his army was defeated by the Mughals after he attempted to defend his fort at Gurdas Nangal. He was captured along with 700 of his men and sent to Delhi, where they were all tortured and executed after refusing to convert to Islam.