The date of the founding of the Khwarazmian dynasty remains debatable. During a revolt in 1017, Khwarezmian rebels murdered
Abu'l-Abbas Ma'mun and his wife, Hurra-ji, sister of the
Ghaznavid sultan
Mahmud. In response, Mahmud invaded and occupied the region of Khwarezm, which included Nasa and the
ribat of Farawa. As a result,
Khwarezm became a province of the
Ghaznavid Empire from 1017 to 1034. In 1077, the governorship of the province, which since 1042/1043 belonged to the
Seljuqs, fell into the hands of
Anush Tigin Gharchai, a former
Turkic slave of the Seljuq sultan. In 1141, the Seljuq Sultan
Ahmed Sanjar was defeated by the
Qara Khitai at the
battle of Qatwan, and Anush Tigin's grandson
Ala ad-Din Atsiz became a vassal to
Yelü Dashi of the
Qara Khitan. Sultan Ahmed Sanjar died in 1156. As the Seljuk state fell into chaos, the Khwarezm-Shahs expanded their territories southward. In 1194, the last Sultan of the Great Seljuq Empire,
Toghrul III, was defeated and killed by the Khwarezm ruler
Ala ad-Din Tekish, who conquered parts of
Khorasan and western Iran. In 1200, Tekish died and was succeeded by his son,
Ala ad-Din Muhammad, who initiated a conflict with the
Ghurids and was defeated by them at Amu Darya (1204). Following the sack of Khwarizm, Muhammad appealed for aid from his suzerain, the
Qara Khitai who sent him an army. With this reinforcement, Muhammad won a victory over the
Ghurids at Hezarasp (1204) and forced them out of Khwarizm. Ala ad-Din Muhammad's alliance with his suzerain was short-lived. He again initiated a conflict, this time with the aid of the
Kara-Khanids, and defeated a Qara-Khitai army at
Talas (1210), but allowed
Samarkand (1210) to be occupied by the Qara-Khitai. He overthrew the
Karakhanids (1212) and
Ghurids (1215). In 1212, he shifted his capital from
Gurganj to
Samarkand. Thus incorporating nearly the whole of
Transoxania and present-day
Afghanistan into his empire, which after further conquests in western Persia (by 1217) stretched from the
Syr Darya to the
Zagros Mountains, and from the northern parts of the
Hindu Kush to the
Caspian Sea. By 1218, the empire had a population of 5 million people. ==Anushteginid Khwarazmshahs==