In 1757, the Afghan ruler, Ahmad Shah Durrani, sacked Delhi. He returned to Afghanistan leaving a Mughal puppet ruler in nominal control. The Marathas again occupied Delhi in 1758, and were in control until their defeat in 1761 at the third battle of Panipat when the city was captured again by Ahmad Shah. However, in 1771, the Marathas established a protectorate over Delhi when the Maratha ruler, Mahadji Shinde, recaptured Delhi and the Mughal Emperor Shah Alam II was installed as a puppet ruler in 1772. In 1783, Sikhs under Baghel Singh captured Delhi and Red Fort but due to the treaty signed, Sikhs withdrew from Red Fort and agreed to restore Shah Alam II as the emperor. In 1803, during the Second Anglo-Maratha War, the forces of British East India Company defeated the Maratha forces in the Battle of Delhi.
During the Indian Rebellion of 1857, Delhi fell to the forces of East India Company after a bloody fight known as the Siege of Delhi. The city came under the direct control of the British Government in 1858. It was made a district province of the Punjab.