'Alā' al-Din Muhammad II (Persian: علاءالدین محمد خوارزمشاه; full name: Ala ad-Dunya wa ad-Din Abul-Fath Muhammad Sanjar ibn Tekish) was the Shah of the Khwarazmian Empire from 3 August 1200 to December 1220. His ancestor was Anushtegin Gharchai, a Turkic Ghulam who eventually became a viceroy of a small province in Central Asia named Khwarazm. The expansion seen under the rule of Muhammad constituted the pinnacle of the developing Khwarazmian military culture, and in various campaigns of conquest created a realm extending from Shirvan to the Indus river and the outer periphery of Mongolia after defeating his adversaries, namely the Qara Khitai, Ghurids, and Eldiguzids. He was able to rally as much as 400,000 men, and with these massive tribal armies his empire was as large as 2,400,000-3,600,000 km². By 1218, he brought the empire to its political and military zenith, but after a skirmish with the Mongols, Muhammad's realm was destroyed when they entered into Transoxiana and sacked Gurganj, Bukhara, his capital in Samarkand, Otrar, Herat among others. Muhammad died on the Caspian island of Ashur-Ada in December 1220.

Early life Ala ad-Din Muhammad II was born Qutb ad-Din Muhammad in Gurganj and served in the court of his father Ala al-Din Tekish. He served as wali of Merv upon his appointment in December 1193, and became the heir to the throne in Spring 1197 when the eldest, Malik-Shah, passed away. Throughout the last years of Tekish's rule, Muhammad accompanied him both in diplomatic missions and military campaigns; Muhammad personally led an expedition against the Ismaili fort of Turshiz in early 1200, concluding a peace deal in which 100,000 dinars were given as concession to the Khwarazmshah.

Reign After his father Tekish died on 3 July 1200, Muhammad succeeded him. The coronation took place on 3 August 1200. He took up the name Ala ad-Din Muhammad and the penname Sanjar II.

Muhammad's nephew, Hindu-Khan went from Nishapur where he served as wali to Merv, intentionally missing the coronation ceremony and gathering forces there. He requested support from Ghiyath al-Din Muhammad of Ghor, an overture that was accepted; Muhammad reciprocated to this open act of desertion by dispatching an army under Amir Chaqir; Hindu-Khan fled to Ghor and Merv was recaptured including Hindu-Khan's children and wives.

Simultaneously, Ardashir, the ispahbadh of the Bavandid realm in Mazandaran captured Ustunavand, Damghan, Folui and Firuzkuh, sabotaging Khwarazmian garrisons in Persian Iraq, which fell into anarchy until its seizure by the Eldiguzids 1203-4 when a former Mamluk of atabeg Jahan Pahlawan invaded and defeated and killed the most powerful local ruler, Izz ad-Din Gokcha/Kukjeh.

Fall of Khorasan

Muhammad recalled his brother, Taj ad-Din Ali-Shah from the western frontier in 1201 in order to support the defense of Khorasan as swarms of Ghurid troops began overrunning the region. Muhammad ibn Kharnak, Ghiyath's governor of Taliqan besieged Merv and was joined by Muizz al-din Muhammad and Ghiyath with Indian war elephants shortly after, causing it to fall when Amir Chaqir surrendered the fortress and was exiled in Ghurid captivity. Throughout early 1201, Sarakhs, Nasa, and Abivard fell, which were given to a cousin of Ghiyath, Emir Zangi ibn Mas'ud. Tus fell following this after a 3-day siege. On 7 April 1201, the Ghurids under Muizz al-Din besieged Nishapur under Taj ad-Din 'Ali Shah, which surrendered a few days later. The city was plundered for an hour before Ghiyath received word and ordered everything looted be restored. Some Khwarazmian troops hid in the Friday mosque, but it was breached and they were captured. Ghiyath went to Herat and appointed DIya al-Din Muhammad ib Abi' Ali al-Ghori, another of his cousins, to govern Khorasan militarily and financially. Muizz al-Din led one last series of incursions in Khorasan in 1201 where he attacked and destroyed an Isma'ili village in Quhistan massacring and plundering it, and then besieged another nearby Isma'ili fortress. Their leader sent a complaint to Ghiyath al-Din Muhammad, who had returned to Afghanistan, likely Firozkoh. Ghiyath sent a messenger who ordered Muizz to depart while he was besieging one of their towns, which he was angrily forced to agree to, proceeding to Ghazna, and then India.

Ala ad-Din Muhammad had finally gathered his army and completed organizing his government, departing from Gurganj and recaptured Abivard and Nasa in 1201. Hindu-Khan, who had been re-instated in Merv, fled to Ghiyath in Firozkoh. Muhammad continued expanding in Khorasan and captured Merv in September 1201, besieging Nishapur on the 18th. Diya ad-Din al-Ghuri was forced to surrender after a two-month siege, and Muhammad allowed the garrison to return home, destroying the city fortifications. Somewhere in 1201–1202, Ala ad-Din Muhammad attempted to capture Sarakhs from Emir Zangi ibn Mas'ud, but was deceived when the Ghurids said they would surrender if allowed to leave the fortress, but they simply gathered food from around the city, returned behind the walls, and locked them. Muhammad withdrew and left some emirs to continue the siege, but then, Muhammad ibn Kharnak of Taliqan them marched on Sarakhs to surprise the Khwarazmians, but they caught wind of it and withdrew. Zanki and Kharnak regrouped at Marw al-Rudh, and Muhammad sent an army under his maternal uncle with 3,000 cavalry, which was defeated.

Muhammad sent a peace offer after hearing of this, and Ghiyath sent an emir, al-Husayn ibn Muhammad al-Marghani, but Muhammad was only stalling for time, and arrested him. In early 1202, Muhammad then marched on Herat which he besieged against the Emir, 'Umar ibn Muhammad al-Marghani. However, al-Husayn, who had just been arrested, manage to relay intel on Muhammad's march in advance, allowing the Ghurids to form a defense while awaiting support from another army, which arrived in the form in Alp Ghazi, a nephew of Ghiyath al-Din, while the latter himself marched to Herat via Firuzkuh and encamped at Razin nearby. Despite this, numbers were fairly equal, but the favor changed when Muhammad sent a force to raid Taliqan, but was destroyed by a Ghurid general, al-Husayn ibn Kharmil, while Muizz al-Din had been recalled from India to Ghazna and arrived between the 28 March and 26 April 1202, where he went to Balkh, then Bamyan, and then towards Merv where he set his march. The Khwarazmshah was forced to relent from his siege, sending a small tribute for peace to Emir al-Marghani before withdrawing to Merv. Muizz al-Din soon arrived in Merv, where he and Muhammad's advance guards skirmished, and heavy losses were sustained. Eventually, Muhammad chose to abandon the battle, and Merv was occupied by the Ghurids again. Muizz al-Din then went to Tus and wintered there, but then, Ghiyath al-Din Muhammad died in January 1203 which upended the situation, and he went to Herat and left Muhammad ibn Kharnak in charge of Merv.

Victory over the Ghurids

The procession was held in March–April and Ghiyath's death was concealed for the time being, while some of Ala ad-Din Muhammad's emirs attempted to attack Merv, but were defeated; Muhammad then sent an army under Barfur the Turk, who defeated the Ghurids under Kharnak, who moved to defend the region outside of Merv, forcing them to give up the city and having Kharnak executed. Muizz al-Din was furious about this and a series of envoy exchanges were made between him and Muhammad, but no settlement was reached. Nonetheless, the new Ghurid Sultan temporarily refrained from combat; he went to Ghazan and appointed Alp Ghazi in Herat and Falak al-Mulk in Firuzkuh and Khorasan. In January 1204, while Muizz al-Din was preoccupied with a revolt in Lahore, Muhammad marched on Herat and laid siege to the city from March to the 2 May 1204, inflicting serious losses. During this time, he also sent 1,000 cavalry against Al-Husayn ibn Kharmil at Kurzaban who feigned surrender if a force was sent, but that contingent was destroyed. Muhammad nonetheless forced Alp Ghazi to surrender in Herat; an indemnity was taken but he withdrew from the city, and Alp Ghazi died soon after. Muhammad then attacked the Badghis east of Herat successfully, seizing loot. But, by May, Muizz al-Din had arrived, infuriated by the recent developments, so Muhammad first went to Sarakhs, and then Merv before beginning defense preparations against the new Ghurid invasion. Muhammad divided his army and raced Muizz to Khwarazm, flooding the direct route to Gurganj, forcing the Ghurids to wait forty days before repairing it and having the floods clear. Near the Qarasu Canal, Ala ad-Din Muhammad suffered a defeat in a pitched battle against Muizz al-Din, and the Ghurids killed their Khwarazmian prisoners.

Muhammad appealed to the Qara Khitai to save him, which they agreed to. The Ghurids besieged Gurganj, resulting in a struggle for the city. The leader of the Shafi'ites in Khwarazm, Shihab ad-Din al-Khivaqi helped rally the defenses and donated his property to fund it, while Muhammad's mother Terken Khatun helped reorganize the defense and arm the locals, with an army of 70,000 assembled. Muizz al-Din was eventually forced to retreat after the Qara Khitai army arrived, and he was pursued to Andkhud by them and the Khwarazmians. On 28 September 1204, he skirmished with the Qara Khitai and on 29 September, the Battle of Andkhud took place during which the Ghurids were defeated. Muhammad returned to Khwarazm and signed a truce on the condition of the Ghurid withdrawal from Khorasan and cession of Marw al-Rudh, while Muizz al-Din was besieged in Andkhud, being forced to cede treasures including one war elephant (although he kept Balkh) due to turmoil in his home territory and rumors of his death.

In April 1205, the window of peace was briefly interrupted when Ghurid commander Amir Zangi assaulted Marw al-Rudh, but he was repelled and killed in battle by Taj ad-Din 'Ali Shah and Jaqar, with his head exhibited in Merv for several days after. Muhammad also re-instated tribute payments to the Qara Khitai as recompense for their help.

Final war with Ghurids After the defeat of the Ghurids, another war was fought with the Bavandids around this time when a succession crisis involving Rukn ad-Dawla Qarin and his brother Rustam V took place. The former appealed to the Khwarazmians for aid, which was accepted; an army under Taj ad-Din Ali-Shah was sent which managed to force Rustam to give up most of his domains to Rukn including Amol with the exception of the fortress of Kuza; Ali-Shah was appointed to supervise Tabaristan and Jurjan, and Mazandaran between 1205 and 1210 after Rustam died. Rukn was killed by the Ismailis who seized some territory here and there, so Rustam V remained as client ruler for the time being.

On 13 March 1206, an opportunity arose as Muizz al-Din Muhammad, who had ruled Ghor and fought numerous wars with the Khwarazmshah, was assassinated. The Ghurids subsequently fragmented; Delhi went under Qutb ad-Din Aybak, Multan under Nasir ad-Din Qubacha, Zabulistan and Ghazna under Taj ad-Din Yildiz, and Herat and Firuzkuh under Ghiyath ad-Din Mahmud (son of Ghiyath ad-Din Muhammad). Soon after, 'Izz ad-Din Husayn ibn Khurmil, the governor of Herat ostensibly serving Ghiyath ad-Din Mahmud, submitted to Muhammad after reports from Ghiyath arrived ordering him to submit. Muhammad had 'Izz ad-Din send his son as a hostage and Khwarazmian troops entered the city, placing their occupation over it.