In June 1249, the Seventh Crusade under Louis IX of France landed in Egypt and took Damietta. The Egyptian troops retreated at first, spurring the sultan to hang more than 50 commanders as deserters. When the Egyptian sultan As-Salih Ayyub died, the power passed briefly to his son Turanshah and then his favorite wife Shajar Al-Durr (or Shajarat-ul-Dur). She took control with mamluk support and launched a counterattack. Troops of the Bahri commander Baibars defeated Louis's troops. The king delayed his retreat too long and was captured by the Mamluks in March 1250, and agreed to a ransom of 400,000 livres (150,000 of which were never paid).[7] Political pressure for a male leader made Shajar marry the mamluk commander Aybak; he was later killed in his bath, and in the power struggle that ensued vice-regent Qutuz took over. He formally founded the first Mamluk sultanate and the Bahri dynasty.
...
When the Mongol Empire's troops of Hulagu Khan sacked Baghdad in 1258 and advanced towards Syria, Mamluk Emir Baibars (Turkish: Baybars)( Circassian:Bipars, a common Circassian name which means the frontier defending warrior) left Damascus for Cairo where he was welcomed by Sultan Qutuz.[8] After taking Damascus, Hulagu demanded that Qutuz surrender Egypt but Qutuz had Hulagu's envoys killed and, with Baibars' help, mobilized his troops. Although Hulagu had to leave for the East when great Khan Möngke died in action against the Southern Song, he left his lieutenant, the Christian Kitbuqa, in charge. Qutuz drew the Mongol army into an ambush near the Orontes River, routed them at the Battle of Ain Jalut and captured and executed Kitbuqa (see Qutuz).
After this great triumph, Qutuz was assassinated by conspiring Mamluks. It was said that Baibars, who seized power, was involved in the assassination. In the following centuries the rule of mamluks was discontinuous, with an average span of seven years.
The Mamluks defeated the Mongols a second time in Homs in 1260 and began to drive them back east. In the process they consolidated their power over Syria, fortified the area, formed mail routes, and formed diplomatic connections between the local princes. Baibars's troops attacked Acre in 1263, captured Caesaria in 1265, and took Antioch in 1268.