The Turks had been living to east of the Caspian Sea for a long time. Around the turn of the first millennium AD, their Central Asian homeland experienced a drastic change in climate, creating the need for more suitable pasture land. So Turkish tribes started pouring into Iran proper. Before 1040, the rulers of Iran, the Ghaznavids, decided to put a stop to this migration, apparently because these tribes were disrupting the local Iranian economy. The Turks didn’t go for that and gave the Ghaznavids a beating in the Battle of Dandanaqan. Wikipedia portrays the battle as one between the Ghaznavids and the ‘Seljuk Empire’, when it was really the Ghaznavids facing a coalition of Turkish tribes. Very mobile Turkish tribes. After this defeat, the gates were open for the Turks.
Heading West, Iran’s mountainous geography basically forces any invaders toward northern Iraq, through the central lowlands, which is where the Turkish tribes ended up. From there they simply spread south and west. South brought them in control of Syria and Baghdad, the seat of the Caliphate, while westward they encountered the Byzantine territories.
The story of how the Turks spread out across much of the Old World is an amazing one. By the 18th century, everything between Xinjiang and Bengal in the East, and Hungary and Algeria in the West, was ruled by people of Turkish stock. And while it didn’t exactly start with that border conflict in Eastern Iran, because Turks had long been present in the Muslim world as elite slave troops and slave-sultans (it’s a real thing, Mamluk), that border conflict kick-started a Turkish era for much of Eurasia and North Africa.