In 1261, following the Mongol conquest of Baghdad, the Mamluk rulers of Egypt tried to gain legitimacy for their rule by declaring the re-establishment of the Abbasid caliphate in Cairo. The Abbasid caliphs in Egypt had little political power; they continued to maintain the symbols of authority, but their sway was confined to religious matters. The first Abbasid caliph of Cairo was Al-Mustansir (r. June–November 1261). The Abbasid caliphate of Cairo lasted until the time of Al-Mutawakkil III, who ruled as caliph from 1508 to 1516, then he was deposed briefly in 1516 by his predecessor Al-Mustamsik, but was restored again to the caliphate in 1517.