In the mid-17th century, the Mughal Emperor Shah Jahan(1628–1658) built the city that sometimes bears his name Shahjahanabad, the seventh city of Delhi that is more commonly known as the old city or old Delhi. This city contains a number of significant architectural features, including the Red Fort (Lal Qila) and the Jama Masjid. The old city served as the capital of the later Mughal Empire from 1638 onwards, when Shah Jahan transferred the capital back from Agra. Aurangzeb (1658–1707) crowned himself as emperor in Delhi in 1658 at the Shalimar garden ('Aizzabad-Bagh) with a second coronation in 1659. After 1680, the Mughal Empire's influence declined rapidly as the Hindu Maratha Empire rose to prominence.
In 1737, Maratha forces sacked Delhi, following their victory against the Mughals in the First Battle of Delhi. In 1739, the Mughal Empire lost the huge Battle of Karnal in less than three hours against the numerically outnumbered but military superior Persian army led by Nader Shah of Persia during his invasion after which he completely sacked and looted Delhi, the Mughal capital, carrying away immense wealth including the Peacock Throne, the Daria-i-Noor, and Koh-i-Noor. The Mughals, severely further weakened, would never overcome this crushing defeat and humiliation which would also let the way open for more invaders to come, including eventually the British. Nader eventually agreed to leave the city and India after forcing the Mughal emperor Muhammad Shah I to beg him for mercy and granting him the keys of the city and the royal treasury. A treaty signed in 1752 made Marathas the protector of the Mughal throne at Delhi. In January 1757, Abdali invaded Delhi. He returned to Afghanistan in April 1757 giving the control of Delhi to Najib-ud-Daula. However, Marathas occupied Delhi after defeating Najib in a siege of the city. In 1761, the Marathas lost Delhi as a consequence of the third battle of Panipat, the city was again raided by Abdali.