Afghan-Maratha War
The Afghan-Maratha War was fought between the Afghan Empire under Ahmad Shah Durrani and the Maratha Empire and the Sikh Confederacy between 1758 and 1761. It took place in north-west India, primarily the region around Delhi and Punjab. The three year long war ended in a catastrophic defeat for the Maratha Confederacy and north-western Indian subcontinent fell under the control of the Afghan Empire. Eastern parts of the Punjab were under the control of the militant Sikh Confederacy which continued waging wars against the Afghan Empire and later Emirate of Afghanistan following Maratha defeat in the Afghan-Maratha War. Delhi under the Mughal dynasty was placed under the occupation of the Kingdom of Rohilkhand, an Indian kingdom in modern-day western Uttar Pradesh and an ally of the Afghans. Background
After the death of Mughal emperor Aurangzeb in 1707, the Mughal–Maratha Wars ended in victory for the Maratha Empire. This was followed by the phase of rapid expansion of the Maratha Empire into North India for the next 50 years under Peshwa Baji Rao I and his brother Chimaji Appa. They conquered Gujarat, the whole of Central India and Orissa, subdued Rajputana and raided into Bengal and Tiruchirapalli in Tamil Nadu, and imposed chauth upon these areas. Their ambition pushed them further northwards than Delhi into Haryana, which collided with the ambitions of Ahmad Shah Abdali, the founder of the Durrani Empire. In 1757, Ahmad Shah Abdali raided Delhi and captured Punjab and Kashmir with the help of Rohilla chief Najib Khan. He installed his son Timur Shah Durrani in Multan and went back to Afghanistan. Battle of Delhi (1757) 
The Maratha Peshwa Balaji Baji Rao sent his brother Raghunath Rao along with Shamsher Bahadur, Ramsingha, Gangadhar Tatya, Sakharam Bapu Bokil, Naroshankar Rajebahadur, Sidhojiraje Gharge-Desai-Deshmukh, Mankojiraje Gharge-Desai-Deshmukh, Maujiram Bania and a large army towards Delhi. They were accompanied by Malhar Rao Holkar of Malwa who had much experience in North India and with its rulers. The Marathas captured Delhi in August 1757. They decisively defeated the Rohillas and Afghans near Delhi in 1758. The defeat was so decisive that Najib Khan surrendered to the Marathas and became their prisoner. Initial campaign and success 
In the Punjab, Adina Beg Khan, along with the Sikhs revolted against the oppressive Afghans. He decided to request the Maratha support as a large Afghan army was expected to reinforce and Adina needed more alliance to battle the invaders. On 7 March, Raghunathrao had encamped at Rajpura where he received Adina Beg Khan's envoys, and was informed that the latter, accompanied by 15,000 Sikh fighters, belonging to the bands (the jathas) of Jassa Singh Ahluwalia and Baba Ala Singh of Patiala had closed upon Sirhind from the other side of the Satluj. A concerted attack on the fort of Sirhind was made by the Marathas and the Sikhs on 8 March 1758. Ahmad Samad Khan, with his 15,000 Afghan troops, held out for about two weeks before his capitulation on 21 March. After the victory, the town was thoroughly sacked by the victors. Therefore, the victorious allies marched upto Lahore and the city fell after some initial fighting. Then, the allies forced the Afghans into the Khyber Pass. The captured Uzbek, Pashtun and Khorasani soldiers were brutally tortured and forced to clean up the holy temples desecrated by them. The Maratha and Sikh forces gave chase to the Pathans on horseback and were in quick pursuit of them in which they went on to capture Attock and then Peshawar from the Afghans. Maratha general Bapuji Trimbak was given the charge of guarding Multan and Dera Ghazi Khan from the Afghans. Adina Beg's sudden death threw Punjab into turmoil. Many of his soldiers, particularly Afghan mercenaries deserted his army camp and added to the number of freebooters, thus creating chaos and anarchy everywhere. Sikhs started again to revolt against Muslim ruling elite, which had caused Punjab to go into political and economic turmoil. Khawaja Mirza who was now the Maratha governor of Haryana-Delhi could not cope with the situation. He sent an express appeal to the Peshwa for reinforcements, alerted all the junior Maratha officers to help him restore law and order in the state and he also recalled Maratha detachments from Peshawar and Attock to safeguard his position in Karnal. Tukojirao Holkar and Narsoji Pandit, the Maratha commanders of Peshawar and Attock had to withdraw their troops from the frontier posts. Sabaji Scindia was now given the charge of Peshawar. Raghunathrao and Malharrao were not very interested in holding their positions in the north for long. On their request, the Peshwa had to find their substitutes. He gave supreme command of Delhi to Dattaji Scindia, while Jankoji Scindia was appointed his deputy. They proceeded towards Delhi separately at different times. A massive army of Marathas under their new commanders, the Scindias, reached Machhiwara in March 1759. Like Raghunathrao, Dattaji also didn't want to stay in Punjab for long. As there was no news of Abdali's invasion, Dattaji deferred the appointment of any permanent governor in Punjab. After deliberations with his advisors, Dattaji deputed Sabaji to take care of Lahore, Peshawar and Attock along with the assistance of Tukojirao, who was deputed by Malharrao. Other officers and Dattaji himself for now left Punjab for the suppression of Najib-ud-Daula in the Ganges valley. Bapurao Trymbak took the charge of Rohtas Fort, while other officers were appointed on the frontier posts. Taking advantage of Sabaji's absence from Peshawar post, the Afghans marched to Peshawar. The Peshawar fort was taken by Afghans with heavy losses to the besieged Maratha garrison. Thereafter the Afghan invaders, under Jahan Khan overran Attock and threatened Rohtas Fort. By this time, Sabaji Scindia had reached the region in the Battle of Lahore, (1759) with fresh troops and a large number of Sikh fighters, who had once again allied with the Marathas. The combined forces of the Marathas and Sikhs massacred the Afghan garrison in which Jahan Khan lost his son and was himself wounded. The Afghans quickly vacated the forts of Peshawar and Attock and retreated west to Afghanistan. Hence, Peshawar once again fell to the Marathas. Final defeat of the Marathas 
It was unbearable for Abdali to overlook this defeat. Najib-ud-Daulah invited Abdali to avenge his defeat. He, along with his commander Jahan Khan invaded Punjab for the fifth time with a gigantic army of 60,000 men accompanied by heavy field-guns and Zamburaks. Trimbakrao, the Maratha governor of Multan, at the head of 5,000 troops, made a tactical decision of retreating towards Lahore; Sabaji Shinde also vacated Peshawar and was joined by Tukoji Holkar at Attock, backing towards Lahore. The remaining Marathas, along with Sikhs and Jats offered staunch resistance to the invaders at Lahore, but they were ultimately defeated due to inferior numbers. On 24 December 1759, a ferocious battle was fought between Dattaji and Abdali in which Dattaji's general, Sardar Bhoite was defeated with a loss of 250 Maratha soldiers after the Mughal contingent fled from the Maratha side. As a consequence of his victory, Abdali managed to join forces with Najib-ud-Daula. Qutub Shah, the ally of Najib Khan and the religious leader of the Rohillas, killed Dattaji and beheaded him at Burrari Ghat near Delhi in January 1760, in a treacherous ambush. Peshwa Nanasaheb sent his cousin Sadashivrao Bhau to repel Abdali which ultimately culminated in the Third Battle of Panipat where although Abdali won a decisive victory, though at the cost of many troops. Before returning to Afghanistan, Abdali sued for peace with Marathas blaming Najib and others for his entry in India and declared that he despised any rivalry with the Marathas. Abdali re-instated Marathas as the "Protector of the Emperor" at Delhi. Reasons for decline

The Marathas had failed to befriend the important players in Punjab, particularly the Sikhs, as they had gotten close enough to be aided by Sikh troops in numerous battles. They could not make any formal treaty with Sikhs, who along with Adina Beg had assisted them in their conquest of the northwest. According to an assessment, the Sikhs were ever ready to co-operate with the Marathas, but it goes to the discredit of the Marathas that they could not make a proper confederacy with Sikhs due to their minor stature as a confederacy. Sikhs regency was highly fluid until the Marathas arrived winning for them Sirhind and Lahore. The Marathas fought successful wars with both the Sultans of Mysore, namely Hyder Ali and Tipu Sultan, in which both were defeated. The Maratha also fought many wars with the Nizam of Hyderabad and crushed his power. They also fought a war with East India Company in 1775 and defeated them with great difficultly. They were also fighting against the Portuguese near Thane and Surat, moreover their capital was Poona (now Pune) which was too far from Delhi to conduct immediate actions and war play. In brief, the Marathas didn't have peaceful time in their period of supremacy as they always had to face battles after battles in various parts of country, so they didn't get enough time to establish stable administrations in regions which they had conquered in northwest India and Pakistan. They even decided to extend their rule up to Kabul and Kandahar but several Hindu kings feared that the emergence of the Maratha empire would hurt their territorial interests so they invited Abdali to invade India along with Muslim rulers. Finding the Maratha leadership completely off guard against their political foes, many Afghans who were earlier taken captives by Marathas quickly changed their loyalty towards Adina Beg and were recruited in his army. However, later on, they betrayed him and joined Abdali's forces during his fifth invasion. The Peshwa was alarmed by the growing French and British influence in the Deccan. When Abdali invaded Punjab for the fifth time, the Marathas didn't try hard enough to save the frontier posts and instead started planning to save Delhi from another invasion. See also
 Maratha Resurrection
 Capture of Delhi (1771)

References 

Military history of India
Conflicts in 1758

Invasions by India