So I was looking at random things on the internet and found the Ayyubid sultanate in Egypt (1171-1341) and then the Mamluk sultanate of Egypt (1250-1517) I wonder why these dates would collide as they had the same territory.
The dates are somewhat misleading, as the Ayyubids of Syria hung on until 1341, but they only lasted in Egypt until 1250. And you are correct, there is a link: Ayyubids were the Kurdish military dynasty of Nur al-Din and his successor Salah-al-Din (Saladin). The Mamluks were their military slave soldier corps; when the last heir to the Ayyubid throne died, the Mamluks took power for themselves.
The story of how this happened is pretty interesting, as it revolves around Shajar al-Durr, the wife of the last Ayyubid sultan. The Seventh Crusade under the command of Louis IX of France landed in Egypt in the winter of 1249, and was met by an Ayyubid force under the command of the sultan, as-Salih Ayyub. As-Salih was gravely ill and died at Mansura in late November. Shajar al-Durr and as-Salih's top advisor and military commander--who were Aybek Mamluks--decided to maintain the ruse that the sultan was still alive in order to prevent loss of troop morale while his son was brought in from Syria. During this period, Shajar al-Durr was essentially ruling Egypt, with an advisor forging her husband's handwriting.
as-Salih's son, Turanshah (who was not Shajar al-Durr's son) arrived in February and went directly to the battlefield, expelling the Crusaders in April. During the brief period he was in charge, he alienated most of the military leadership and demonstrated poor behavior--notably he was drunk in public rather frequently, and was abusive toward the women in his father's harem. He lasted barely a month before he was assassinated by people loyal to the Mamluk commanders and Shajar al-Durr.
After this, the Mamluks chose to place Shajar al-Durr on the throne of Egypt, with 'Izz ad-Din Aybak as her commander in chief. The caliph in Baghdad refused to acknowledge a woman as sultan of Egypt (she's often referred to as "sultana," but she herself used the title "sultan"), which was a huge blow to her legitimacy (technically the Mamluks were servants of the caliph, and could only be commanded by a sultan appointed by him). So, it was decided that 'Izz ad-Din Aybak would serve as sultan instead, although Shajar al-Durr was openly involved in leadership, going so far as to sit behind his throne behind a screen and whisper orders to him to be repeated to the assembled courtiers.
However, the appointment of a Mamluk as sultan of Egypt didn't sit well with the Ayyubid family, who sent a minor prince as replacement, leading to an open military clash between the Mamluks and Ayyubids, which was eventually settled through mediation by the caliph, granting the Mamluks control over Egypt, southern Palestine, and part of the Syrian coast.
This was the moment when Ayyubid control over Egypt was formally surrendered.
However, once Mamluk sovereignty over Egypt was achieved, tensions broke out between Shajar al-Durr, and 'Izz ad-Din, as both wanted control over the country, and 'Izz ad-Din became concerned that Shajar al-Durr, who was still popular among certain factions of Mamluks, was going to try to make a grab for the throne. He had the chief of the faction that supported her assassinated, and took another wife. In retaliation, Shajar al-Durr had 'Izz ad-Din murdered, at which point 'Izz ad-Din's other wife had her forces imprison Shajar al-Durr, executed, and the body publicly displayed outside the city gates.
Why this hasn't been turned into a Hollywood blockbuster is beyond me.