Human habitation was probably present in and around Delhi during the second millennium BC and before, andcontinuous inhabitation has been evidenced since at least the 6th century BC. The city is believed to be the site of Indraprastha, legendary capital of the Pandavas in the Indian epic Mahabharata. Settlements grew from the time of the Mauryan Empire (c. 300 BC).
Remains of seven major cities have been discovered in Delhi. The Tomara dynasty founded the city of Lal Kot in AD 736. The Chauhans conquered Lal Kot in 1180 and renamed it Qila Rai Pithora. The Chauhan king Prithviraj III was defeated in 1192 by the Afghan Muhammad Ghori.
In 1206, Qutb-ud-din Aybak, the first ruler of the Slave Dynasty established the Delhi Sultanate. Qutb-ud-din started the construction the Qutub Minar and Quwwat-al-Islam (might of Islam), the earliest extant mosque in India. After the fall of the Slave dynasty, a succession of Turkic and Afghan dynasties, the Khilji dynasty, the Tughluq dynasty, the Sayyid dynasty and the Lodhi dynasty held power in the late medieval period, and built a sequence of forts and townships that are part of the seven cities of Delhi.
In 1398, Timur Lenk invaded India on the pretext that the Muslim sultans of Delhi were too lenient towards their Hindusubjects. Timur entered Delhi and the city was sacked, destroyed, and left in ruins. Near Delhi, Timur massacred 100,000 captives. Delhi was a major centre of Sufism during the Sultanate period. In 1526, Zahiruddin Babur defeated the last Lodhi sultan in the First Battle of Panipat and founded the Mughal Empire that ruled from Delhi, Agra and Lahore.
The Mughal Empire ruled northern India for more than three centuries, with a sixteen-year hiatus during the reign of Sher Shah Suri, from 1540 to 1556. During 1553–1556, the Hindu king, Hemu Vikramaditya acceded to the throne of Delhi by defeating forces of Mughal Emperor Akbar at Agra and Delhi. However, the Mughals reestablished their rule after Akbar's army defeated Hemu during the Second Battle of Panipat. Shah Jahan built the seventh city of Delhi that bears his name (Shahjahanabad), and is more commonly known as the "Old City" or "Old Delhi". The old city served as the capital of the Mughal Empire from 1638. After 1680, the Mughal Empire's influence declined rapidly as the Hindu Marathas rose to prominence.
A weakened Mughal Empire lost the Battle of Karnal, following which the victorious forces of Nader Shah invaded and looted Delhi, carrying away many treasures, including the Peacock Throne. A treaty signed in 1752 made Marathas the protector of the Mughal throne at Delhi. In 1761, after the Marathas lost the third battle of Panipat, Delhi was raided by Ahmed Shah Abdali. In 1803, the forces of British East India Company overran the Maratha forces near Delhi and ended the Mughal rule over the city.
After the Indian Rebellion of 1857, Delhi came under direct rule of the British crown and was made a district province of the Punjab. In 1911, the capital of British India was transferred from Calcutta to Delhi, following which a team of British architects led by Edwin Lutyens designed a new political and administrative area, known as New Delhi, to house the government buildings. New Delhi, also known as Lutyens' Delhi, was officially declared as the capital of the Union of India after the country gained independence on 15 August 1947.
During the partition of India, thousands of Hindu and Sikh refugees from West Punjab and Sindh fled to Delhi, while many Muslim residents of the city migrated to Pakistan. Starting on 31 October 1984, approximately three thousand Sikhs were killed during the four-day long anti-Sikh riots after the Sikh body guards of then-Prime Minister, Indira Gandhi, assassinated her. Migration to Delhi from the rest of India continues, contributing more to the rise of Delhi's population than the birth rate, which is declining.
The Constitution (Sixty-ninth Amendment) Act, 1991 declared the Union Territory of Delhi to be formally known as National Capital Territory of Delhi. The Act gave Delhi its own legislative assembly, though with limited powers. In December 2001, the Parliament of India building in New Delhi was attacked by armed militants resulting in the death of six security personnel. India suspected the hand of Pakistan-based militant groups in the attacks resulting in a major diplomatic crisis between the two countries. Delhi again witnessed terrorist attacks in October 2005 and September 2008 resulting in the deaths of 62 and 30 civilians respectively.