Babur, a descendant of both Timur and Genghis Khan, arrived from Central Asia and captured Kabul from the Arghun Dynasty, and from there he began to seize control of the eastern Afghan territories. He remained in Kabul until 1526 when he and his army invaded Delhi in India to replace the Lodi dynasty with the Mughal Empire. From the 16th century to the early 18th century, the region of Afghanistan was contended by 3 major powers: The Khanate of Bukhara ruled the north, Safavids the west and the remaining larger area was ruled by Delhi Sultanate of India.
In 1738, Nader Shah and his army, which included Ahmad Khan and four thousand of his Abdali Pashtuns, captured Kandahar from the last Hotak ruler; in the same year he occupied Ghazni, Kabul and Lahore. In June 1747, Nadir Shah was assassinated by one of his officers and his kingdom fell apart. Ahmad Shah Abdali called for a loya jirga ("grand assembly") to select a leader among his people, and in October 1747 the Pashtuns gathered near Kandahar and chose him as their new head of state. Ahmad Shah Durrani is often regarded as the founder of modern Afghanistan. After the inauguration, Ahmad Shah adopted the title padshah durr-i dawran ('King, "pearl of the age") and the Abdali tribe became known as the Durrani tribe thereafter. By 1751, Ahmad Shah Durrani and his Afghan army conquered the entire present-day Afghanistan, Pakistan, Khorasan and Kohistan provinces of Iran, along with Delhi in India. He defeated the Sikhs of the Maratha Empire in the Punjab region nine times; one of the biggest battles was the 1761 Battle of Panipat. In October 1772, Ahmad Shah retired to his home in Kandahar where he died peacefully and was buried at a site now adjacent to the Mosque of the Cloak of the Prophet Mohammed. He was succeeded by his son, Timur Shah Durrani, who transferred the capital of their Afghan Empire from Kandahar to Kabul. Timur died in 1793 and was finally succeeded by his son Zaman Shah Durrani.
Zaman Shah and his brothers had a weak hold on the legacy left to them by their famous ancestor. They sorted out their differences through "round robin of expulsions, blindings and executions", which resulted in the deterioration of the Afghan hold over far-flung territories, such as Attock and Kashmir. Durrani's other grandson, Shuja Shah Durrani, fled the wrath of his brother and sought refuge with the Sikhs. After Durrani Vizier Fateh Khan was defeated at the Battle of Attock, he fought off an attempt by Ali Shah, the ruler of Persia, to capture the Durrani province of Herat. He was joined by his brother, Dost Mohammad Khan, and rogue Sikh Sardar Jai Singh Attarwalia. Once they had captured the city, Fateh Khan attempted to remove the ruler Mahmud Shah – a relation of his superior – and rule in his stead. In the attempt to take the city from its Durrani ruler, Dost Mohammad Khan's men forcibly took jewels from a princess and Kamran Durrani, Mahmud Shah's son, used this as a pretext to remove Fateh Khan from power, and had him tortured and executed. While in power, however, Fateh Khan had installed 21 of his brothers in positions of power throughout the Durrani Empire. After his death, they rebelled and divided up the provinces of the empire between themselves. During this turbulent period, Kabul had many temporary rulers until Fateh Khan's brother, Dost Mohammad Khan, captured Kabul in 1826.
The Sikhs, under Ranjit Singh, rebelled in 1809 and eventually wrested from the Afghans a large part of the Kingdom of Kabul (present day Pakistan, but not including Sindh). Hari Singh Nalwa, the Commander-in-Chief of the Sikh Empire along its Afghan frontier, invaded the Afghan territory as far as the city of Jalalabad. In 1837, the Afghan Army descended through the Khyber Pass on Sikh forces at Jamrud. Hari Singh Nalwa's forces held off the Afghan offensive for over a week – the time it took reinforcements to reach Jamrud from Lahore.First Anglo-Afghan War (1839–42). William Brydon was the sole survivor of a group of 3,600 soldiers of the British 44th Regiment of Foot and 12,400 camp followers, who were attacked between Kabul and Jalalabad while heading to what is now Pakistan. During the 19th century, following the Second Anglo-Afghan War and the ascension of the Barakzai dynasty, Afghanistan saw much of its territory and autonomy ceded to British India. Ethnic Pashtun territories were divided by the 1893 Durand Line, an action which would lead to strained relations between Afghanistan and British India (later the new state of Pakistan). The United Kingdom exercised a great deal of influence, and it was not until the reign of King Amanullah Khan in 1919 that Afghanistan re-gained independence over its foreign affairs after the signing of the Treaty of Rawalpindi. King Amanullah moved to end his country's traditional isolation in the years following the Third Anglo-Afghan War. He established diplomatic relations with major states and, following a 1927-28 tour of Europe and Turkey, introduced several reforms intended to modernize his nation. A key force behind these reforms was Mahmud Tarzi, an ardent supporter of the education of women. He fought for Article 68 of Afghanistan's first constitution, which made elementary education compulsory. Some of the reforms that were actually put in place, such as the abolition of the traditional Muslim veil for women and the opening of a number of co-educational schools, quickly alienated many tribal and religious leaders. Faced with overwhelming armed opposition, Amanullah Khan was forced to abdicate in January 1929 after Kabul fell to rebel forces led by Habibullah Kalakani. Prince Mohammed Nadir Shah, Amanullah's cousin, in turn defeated and killed Habibullah Kalakani in October 1929, and was declared King Nadir Shah. He abandoned the reforms of Amanullah Khan in favor of a more gradual approach to modernisation. In 1933, however, he was assassinated in a revenge killing by a Kabul student.