The site of Khan el-Khalili was originally the site of the mausoleum known as the turbat az-za'faraan (Saffron Tomb), which was the burial site of the Fatimid caliphs.The mausoleum was part of the Fatimid Great Eastern Palace complex, begun in 970 AD by Gawhar al-Siqilli, the general who conquered Egypt for the Fatimid dynasty and founded Cairo that same year.
By the time of Sultan Barquq, the first Circassian (or Burji) Mamluk Sultan in the late 14th century, Egypt had been significantly affected by the ravages of the Black Death but continued to be the center of great economic activity, and many commercial and religious buildings were constructed at this time. During Barquq's first reign (1382-1389), his Master of the Stables (amir akhur), the amir Jaharkas al-Khalili, destroyed the Fatimid cemetery to erect a large caravanserai (خان khan in Arabic; a building that could house merchants and their goods) at the heart of the city, disposing of the bones of the Fatimid royal family by throwing them into the rubbish hills east of the city.
The khan was located at the middle of Cairo's most important zone of economic activity, and later sultans also built commercial establishments nearby, including Sultan Qaytbay's wikala (another term for caravanserai) south of the Al-Azhar Mosque, and Sultan al-Ghuri's better-preserved wikala just east of his mausoleum complex. This economic and commercial zone ran along the city's main north-south axis, the qasaba (now known as al-Mu'izz street), and was also the privileged site of many monumental religious complexes built throughout the Mamluk period and beyond. By the late 15th century, the district around Khan el-Khalili had also become the major center of foreign trade, which included the sale of slaves and precious stones.
Sultan al-Ghuri, Egypt's last important Mamluk sultan (1501-1516), modified the layout of the district through a major campaign of demolition and new construction. Along with his religious and funerary complex and a large wikala that still exist today, he demolished the original khan built by al-Khalili and rebuilt it in 1511 as a commercial complex with monumental gates and perpendicular streets, resembling the similar establishments in Ottoman cities where the most precious goods were sold. Among al-Ghuri's constructions was the Wikala al-Qutn ("of cotton"), parts of which are still visible today, including an ornate gate and upper stories whose external facade is lined with iron-grilled windows where merchants' rooms were located. Two other monumental gates, the Bab al-Badistan and the Bab al-Ghuri, also date from this time.
From al-Ghuri's reign onwards, the district became associated with Turkish merchants, and during the Ottoman period the Turkish community of Cairo established itself here.