In ancient times, the state of Gandhara occupied the Vale of Peshawar and adjoining areas. This kingdom was important because of its strategic location at the eastern end of the Khyber Pass. Gandhara was annexed by the Persian Achaemenian dynasty in the early 6th century bce and remained a Persian satrapy until 327 bce. The region then passed successively under Greek, Indian, Indo-Bactrian, Sakan, Parthian, and Kushan rule.
Muslim rule was first brought to the region by a former Turkish slave-soldier (mamlūk) named Sebüktigin, who gained control of Peshawar in 988 ce. His son, Maḥmūd of Ghazna, invaded northern India several times between 1001 and 1027 and brought a large area of the present-day province into the boundaries of the Ghaznavid dynasty. Beginning in the late 12th century, the region was held successively by the Ghūrid sultanate, by various Muslim Afghan dynasties, and then by the Mughal dynasty. After the invasion of the Iranian ruler Nādir Shah in 1738, the territory remained under the loose control of the Afghan Durrānī clan. Beginning about 1818, invading Sikhs from the Punjab region of India increasingly secured control of the frontier territory until the coming of the British in 1849.
The northwestern frontier areas were annexed to India by the British after the Second Sikh War (1848–49). The territories thenceforth formed a part of the Punjab until the province, then known as North-West Frontier Province, was created in 1901. After Pakistan attained independence in 1947, the region continued to exist as a separate Pakistani province. However, the inhabitants of the tribal territories, the westernmost area along the Afghanistan border, were not made subject to the Pakistani legal code. During the 1980s the province was inundated by Afghan refugees seeking asylum from the Soviet occupation of their country. The tribal areas were also a major staging area for mujahideen fighters entering Afghanistan (see Afghan War). Following the collapse of the Taliban regime in Afghanistan in 2001, the tribal areas became a refuge for Taliban and mujahideen fighters.
In 2010 the name of the province was officially changed to Khyber Pakhtunkhwa. Later that year, unusually heavy monsoon rains forced the Indus River into extraordinary floods. The inundation devastated swaths of Pakistani land, including Khyber Pakhtunkhwa. Officials there, unprepared to deal with such flooding with so little notice, were overwhelmed by the destruction and human costs wrought by the floodwaters. By mid-August, Khyber Pakhtunkhwa accounted for about two-thirds of all reported flood-related deaths.