Source: here.
Nasr
In 914 CE, Nasr ibn Ahmed II raised the Samanid Moslems to their peak. They were the first Iranians who had been converted to Sunnism and pursued their zealous Jihad on central Asia with a large army of Iranian Moslems and Arab mercenaries to convert Turkic pagans, the Hindu Vijayas and Iranian fire worshipers to Islam.
Nasr converted to Shi’aism and this sparked off a rebellion in the Samanid kingdom, as the Sunni and Shia factions went to war on each other. The Shias were aided by the Buyids another dynasty of Iranian Shia warlords and there were pitched battles fought around the Tehran region. Nasr was succeeded by his son Sultan Noah ibn Nasr, who converted back to Sunnism and led a Jihad agains the Buyids in 943 CE. He also led a great Jihad on the pagan Turks in the Steppes north of Khorasan. In this battle the shamanist Turkic tribes of Cuman and Qarluq clans were beaten by the ghazis and taken as slaves in the condition they would forefeit their foreskins.
Ghazna
A young boy captured in this raid, Alptegin was converted to Islam and sold to the Buyids as a slave. After serving as a water jug carrier he was promoted to the palace guards. Then he was given a horse and made a cavalry soldier due to his military skills. He started organizing other Turkic slaves into a personal Turkic army. During the reign of the next Samanid Sultan Abd al Maliq, Alptegin declared himself governor of Khurasan and tried to place a puppet on the Samanid throne. But the Samanids grouping under Mansur-ibn-Noah attacked him. He was defeated and with his Turkic band retreated to Balkh. There again he was defeated by the Samanids and retreated to Ghazni.
There he was let in with his army by the Hindus. He soon ethnically cleansed the Hindus and made himself Sultan of Ghazni. He raised a Turkic army from the Turkic slaves there, which was largely islamized due to the early age at which they were captured. In 963 Alptegin died and was succeeded by his son Abu Ishaq. Abu Ishaq was a homosexual and his lover was Sebuqtegin. Ishaq accepted the Samanids as his nominal overlords and entrenched himself in Ghazna.
Shamanist uprising
However not all Turk slaves amongst them accepted Islam. Another faction of turks under pagan Turk Bilge Tegin wanted to restore the old shamanist religion and sent emissaries to make common cause with the Hindu Kings of Kabul and Khotan, Trilochanapala and the Vijayan, and the Khitan Mongol Khan who had conquered northern China. On the death of Ishaq, Blige Tegin organized his pagan faction and tried to oust the Islamic faction. The latter were defeated initially and were driven into the fort of Gardiz in near Konduz. The Vijayans cutoff the supply line for the Moslem faction from the Samanids. But the Samanids made a vigorous counter assault directly on the Vijayans and sent another army to help Gardiz. Bilge Tegin was shot by an arrow aimed from the fort just as he was about to storm it and killed. He was succeeded by the Qara Tegin and Bori Tegin who continued the struggle aided by the Hindus from Kabul.
The Moslem faction was defeated and dispersed but they regrouped under Sebuktegin, a virulent Islamist. He called for a great Jihad on the pagan Turks as well as the Hindus. He routed the army of Bori Tegin after cutting off the Hindu supplies and beheaded the shamanist. He then outflanked the Hindus by seizing Kandahar in the 990s.
Qarakhanids
In the meantime, the Qarakhanids were another band of Turks of the Toquz Oghuz (the same clan that gave rise to the later Ottomans) who had been converted to Islam by Mullahs sent into their kingdom by the Samanids for subversion. The Qarakhanids in steppes of Kashgaria had entrenched themselves and raised and army to threaten the Samanids. Sure enough in 992 the Qarakhanid sultan, Bughra Khan Harun invaded Bokhara.
The Samanid Sultan Noah II ran to Sebuktegin for aid, who promptly protected the Samanid but in turn seized Khorasan for himself. The Sebuqtegin died and was succeeded by his wild son Mahmud. Mahmud and the Qara Khanid Sultan Arslan Ilek Nasr made a common cause against the Samanids: Turks against the Iranians. Mahmud struck first in 999 and destroyed the Samanid army in the battle of Merv. From the north Ilek Nasr struck and conquered Bokhara after capturing the last Samanid sultan. The two sultans then sealed their alliance as Mahmud gave his daughter to Ilek Nasr’s son. With that the Turkic character of central Asia had been brought into the Islamic world.