In 1400 Timur and his victorious forces invaded Syria, and the new sultan of Egypt, Faraj, went out to meet them, taking Ibn Khaldūn and other notables with him. Shortly thereafter, the Mamlūk army returned to Egypt, leaving Ibn Khaldūn in besieged Damascus. The situation soon becoming hopeless, the civilian notables of the city started negotiations with Timur, during the course of which he asked to meet Ibn Khaldūn. The latter was thereupon lowered over the city wall by ropes and spent some seven weeks in the enemy camp, of which he has given a detailed description in his autobiography.
Timur treated him with respect, and the historian used all his accumulated worldly wisdom and courtly flattery to charm the ferocious world conqueror. Probably dreaming of further conquests, Timur asked for a detailed description of North Africa and got not only a short lecture on that subject, on the caliphate, and on ʿasabiyyah but also an extensive written report. Ibn Khaldūn took advantage of Timur's good mood to secure a safe-conduct for the civilian employees left in Damascus and permission for himself to return to Egypt but not before he witnessed the sack of the city and the burning of its great mosque.
After an exchange of gifts with Timur, he headed southward but was robbed and stripped by a band of Bedouin and only with difficulty made his way to the coast. There a “ship belonging to Ibn Osman, the sultan of Rum, stopped, carrying an ambassador to the sultan of Egypt” and took him to Gaza, establishing his only contact with what was soon to become the dominant power in the Middle East—the Ottoman Empire. The rest of his journey to Cairo was uneventful, as indeed were the remaining years of his life. He died in 1406 and was buried in the cemetery outside Bāb al-Naṣr, one of Cairo's main gates.