Iranian History/The Later Ghaznavids and the Seljuqs
Masud I 
Mahmud's son Masud succeeded him on the former's death in 1030. After a shiort civil war, he stabilized himself. He ruled for a decade during which he witnessed the empire fall apart. In 1037, the capital Ghazni was plundered by the Seljuks. In 1039, the Seljuks launched another raid into Ghaznavid territory. Masud I faced them at Dandanaqan near Merv and suffered a crushing defeat. The western territories of the Ghaznavid Empire, inclouding most of Iran, were thus lost to the Ghaznavids. Following this defeat, Masud I's brother Mohammad rebelled and overthrew Masud I capturing the throne. However, his rule lasted only a few months and Masud I recovered the throne. However, a second uprising in 1041 proved more successful and Masud I was captured and killed
Soon after his accession, Mawdud launched a campaign to recover the lands lost to the Seljuqs but failed. In 1043, there was a rebelion by Rajput chiefs who laid siege to Lahore, albeit unsuccessfully. While Mwdud consolidated Ghaznavid hold over Afghanistan, most of the conquests in Iran, India and Central Asia were lost to the Ghaznavids. Mawdud died in 1050 and was successded by Masud II.
Later rulers 
When Masud III died in 1115, a civil war followed. The Seljuqs, seeing their chance, aided one prince against the other. The victorious candidate Shirzad ascended the throne as a vassal of the Seljuqs. During the twelfth century, the Ghurids of Western Afghanistan rapidly rose in power and influence and formed an independent state of their own. In 1151, the Ghurid king, Alauddin Hussain invaded and captured Ghazni. The Ghaznavids shifted their capital to Lahore and ruled till 1186 when their last stronghold Lahore fell to the Ghurid king, Muhammad Ghori. With the capture of Lahore, the rule of the Ghaznavids came to an end.
The Seljuqs 
The Seljuqs were a branch of the Oghuz Turks of Central Asia who converted to Islam and settled in the province of Khorasan in Northern Iran in the 10th century AD. They inherited their name from their semi-legendary ancestor Seljuk or Seljuq who lived in latter half of the 10th century AD and the first half of the 11th century.
Toghrul I 
Toghrul I was the first king of the Seljuqs who established an independent Seljuq state in the 11th century AD. He was a grandson of the legendary Seljuq. He fought his first war against the Ghaznavids on behalf of the Karakhanids in 1025 but was defeated by Mahmud Ghaznavi. After Mahmud's death, Toghrul I invaded Ghazni and broke off the whole western part of the Ghaznavid Empire after inflicting a crushing defeat on the Ghaznavid ruler Masud I at the Battle of Dandanaqan in 1040. Following the successful conquest of Iran from the Ghaznavids, Toghrul I established the Seljuk Empire with its capital at Ray. Between 1040 and 1044, he consolidated his hold on Khorasan and installed Chagri as the Seljuq Governor of Khwarezm. Between 1054 and 1058, he fought the Byzantine Empire and the Fatimids of Anatolia. In 1060, he successfully took Baghdad from the Fatimids on behalf of the Abbasid Caliph. Toghrul died in Ray in 1063. As he did not leave behind any children, he was succeeded by his nephew Alp Arsalan who ascended the throne after vqnuishing another nephew Suleiman.
Alp Arsalan 
Alp Arsalan was one the greatest rulers of the Seljuq dynasty. He converted to Islam at an early age and took the name of Muhammad bin Daud Chaghri.
In his early years, Alp Arsalan served as the Governor of Khorasan. When his predecessor Toghrul I died in 1060, he captured the throne after defeating his opponents Suleiman and Kutalmish. He was ably assisted by Nizam-ul-Mulk regarded as one of the greatest statemen of the time. Soon after his accession he took the fortress of Caesarea Mazaca and conquered Armenia and Georgia.
In 1068, Arsalan invaded the Byzantine Empire and took the Emperor Romanus IV captive. According to an interesting conversation between Alp Arsalan and Romanus IV, Romanus IV told the Seljuq Emperor that he would have killed him or exhibited him had victory been with the Byzantines. In response to this answer, Arsalan let Romanus IV free in order to exhibit his magnanimity.
Alp Arsalan was assassinated by a prisoner in 1072 and was succeeded by his son Malik Shah I.
Malik Shah I 
Jalal-al-Dawlah Malik Shah ruled the Seljuk Empire from 1072 to 1092. He consolidated the conquests made by his father and great-uncle. He inflicted successive defeats on the Byzantines and drove them out of Anatolia. He also fought incessant wars against the Fatimids of Egypt and established his supremacy over Syria. Between 1078 and 1084, Suleyman, the son of Kutalmish tried to establish himself as the independent ruler of Anatolia and crowned himself Sultan of Rum. However, he was defeated and killed by Malik Shah I in 1086. His son Kilij Arsalan I was taken prisoner.
After the death of Malik Shah I, Kilij Arsalan I established himself as the Sultan of Rum and his sons Barkiyaruq established himself as the independent ruler of Iraq, Muhammad I, of Baghdad, and Ahmed Sanjar, of Khorasan, while Nasir ad Din Mahmud or Mahmud I ruled over Persia. However, Mahmud I's rule lasted for a short time. He died in 1094 and was succeeded by his son Barkyaruq. Barkyaruq participated in the First Crusade against the Franks. He died in 1105.
Ahmed Sanjar 
Barkiyaruq was succeeded by Malik Shah II and Mehmed I who held the throne for short periods of time. During this period, Ahmed Sanjar, son of Malik Shah I and a brother of Mahmud I, Malik Shah II and Mehmed I and the ruler of Khorasan, held more power. He succeeded Mehmed I as sultan on the latter's death in 1118 and ruled till 1153. He was considerably successful in campaigns against the Assassins of Alamut which ended with a peace treaty. However, he suffered crushing defeats at the hands of Kara Khitans in the Battle of Qatwan in 1141 and the Oghuz Turks in 1153. While the first battle resulted in the loss of all Seljuk territories to the east of the river Syr Darya, the second brought an end to the Seljuk Empire.