The Ghaznavid dynasty (Persian: غزنویان ġaznaviyān) was a Persianate Muslim dynasty of Turkic mamluk origin, at their greatest extent ruling large parts of Iran, Afghanistan, much of Transoxiana and the northwest Indian subcontinent from 977 to 1186. The dynasty was founded by Sabuktigin upon his succession to rule of the region of Ghazna after the death of his father-in-law, Alp Tigin, who was a breakaway ex-general of the Samanid Empire from Balkh, north of the Hindu Kush in Greater Khorasan.
Sabuktigin's son, Mahmud of Ghazni, declared independence from the Samanid Empire and expanded the Ghaznavid Empire to the Amu Darya, the Indus River and the Indian Ocean in the East and to Rey and Hamadan in the west. Under the reign of Mas'ud I, the Ghaznavid dynasty began losing control over its western territories to the Seljuq dynasty after the Battle of Dandanaqan, resulting in a restriction of its holdings to modern-day Afghanistan, Pakistan (Punjab and Balochistan). In 1151, Sultan Bahram Shah lost Ghazni to the Ghurid king Ala al-Din Husayn.
Two military families arose from the Turkic slave-guards of the Samanid Empire, the Simjurids and Ghaznavids, who ultimately proved disastrous to the Samanids. The Simjurids received an appanage in the Kohistan region of eastern Khorasan. The Samanid generals Alp Tigin and Abu al-Hasan Simjuri competed for the governorship of Khorasan and control of the Samanid Empire by placing on the throne emirs they could dominate after the death of Abd al-Malik I in 961. His death created a succession crisis between his brothers. A court party instigated by men of the scribal class — civilian ministers rather than Turkic generals — rejected the candidacy of Alp Tigin for the Samanid throne. Mansur I was installed instead, and Alp Tigin prudently retired to south of the Hindu Kush, where he captured Ghazna and became the ruler of the city as a Samanid authority. The Simjurids enjoyed control of Khorasan south of the Amu Darya but were hard-pressed by a third great Iranian dynasty, the Buyid dynasty, and were unable to survive the collapse of the Samanids and the subsequent rise of the Ghaznavids.
The struggles of the Turkic slave generals for mastery of the throne with the help of shifting allegiance from the court's ministerial leaders both demonstrated and accelerated the Samanid decline. Samanid weakness attracted into Transoxiana the Karluks, a Turkic people who had recently converted to Islam. They occupied Bukhara in 992, establishing in Transoxania the Kara-Khanid Khanate.