II () and incorporated Azerbaijan into his domain, thus marking the end of the Rawadid dynasty. In 1104, the Seljuk brothers and rivals Berkyaruq () and Muhammad I Tapar () had their peace treaty signed near Maragheh. A year later, Muhammad I visited Maragheh. In 1111/12, a certain Ahmadil ibn Ibrahim ibn Wahsudan was appointed as the ruler of Maragheh. The background of the latter is obscure, but some modern historians consider him to be from the Rawadid dynasty. Ahmadili rule Following Ahmadil's death on 16 May 1116, he was reportedly succeeded by his slave Aq Sunqur, who by 1122 had emerged as a semi-independent subject of the Seljuks. This marked the start of the Ahmadili dynasty, a name they are commonly referred as due to their connection to Ahmadil. Aq Sunqur secretly conspired with the rebel prince Tughril ibn Muhammad, encouraging the latter to invade Maragheh in return for soldiers and aid. The rebellion failed in 1122/23, and led to the dismissal of Aq Sunqur by the Seljuk ruler Mahmud II (). However, Aq Sunqur was soon re-appointed as the governor of Maragheh. In 1150, the Seljuk ruler Ghiyath ad-Din Mas'ud () besieged Maragheh, due to a conflict between Aq Sunqur's son and successor Arslan Aba and another local ruler. The town was captured after two days, but a resolution was soon reached through the mediation of various military leaders. In 1174/75, the Eldiguzid prince (and subsequent ruler) Muhammad Jahan Pahlavan () besieged Maragheh, but was unsuccessful in capturing it. The last Ahmadili ruler of Maragheh was Sulafa Khatun (). She was at Ru'in Dez during the Mongol conquest of Maragheh in 1221. The Mongols "stormed" the city on 30 March 1221 and burned it and killed its inhabitants. Khwarazmian rule In 1225, the Khwarazmshah of the Anushtegin dynasty, Jalal al-Din Mangburni (), reached Maragheh, which he was able to enter without any trouble, due the discontentment of the locals towards the raids and oppression by the Kingdom of Georgia. Mangburni attempted to restore Maragheh to its previous successful state. Mongol rule In 1231, Mongol rule over Maragheh was made definite. After the Mongol ruler Hulagu Khan () had captured Baghdad in 1258, he established his residence in Maragheh. He also had an observatory built under the directorship of Nasir al-Din al-Tusi. Other buildings from this period are lacking, since the first Mongol Ilkhanate rulers lived a semi-nomadic life.