Outside the Turkish Embassy in Baghdad. Last week, Turkish troops rolled across the border and took up positions near Mosul. Photograph by Khalid al-Mousily / Reuters / Landov

Last week, several hundred Turkish troops, backed by tanks and artillery, rolled across the Iraqi border and took up positions near the city of Mosul, which has been held, since last year, by ISIS. The Turks have since reinforced the battalion with warplanes and intelligence officers. At the same time, the airspace over northern Iraq has been closed for most of the past ten days, because Russian cruise missiles, fired from ships on the Black Sea, have been flying through the region—sometimes even over downtown Erbil.

It’s not entirely clear why, or with whose permission, the Turks ordered their soldiers across the border. The Iraqi Prime Minister, Haider al-Abadi, protested loudly. So has the United States. So what happened? And why are the Turks sending their troops into the volatile country, anyway?

The Turkish move into Iraq is the latest in a series of geopolitical flailings by the blustering and impulsive Turkish President, Recep Tayyip Erdoğan. Most of them are related to the civil war in Syria. Since 2011, when the Syrian uprising began, Erdoğan has sought to gain some kind of advantage there, or at least to feel sure that he is backing the right horse. And he’s failed miserably. As much as any other leader in the region, Erdoğan has pushed vigorously for the removal of Syrian President Bashar al-Assad. To see this through, the Turkish government has backed the most extreme rebel groups, including ISIS, allowing and even helping foreign fighters to come into Turkey and cross into Syria. ISIS would never have metastasized as virulently as it has without Turkey’s assistance.

The Turkish policy in Syria matched its efforts elsewhere in the Middle East in the early years of the Arab Spring. Wherever he could, Erdoğan backed parties linked to the Muslim Brotherhood, the Sunni Islamist movement that Erdoğan’s own political party, known by its Turkish initials, A.K.P., grew out of.

The Muslim Brotherhood has been routed across the Middle East, most notably in Egypt, where its leaders are either in prison or dead. In Syria, Assad is standing stronger than he has in months. (Remember when, in 2013, President Obama decided not to use force against Assad after he crossed the “red line” by using chemical weapons? No one was angrier at President Obama than Erdoğan.) Assad—a member of the Alawite sect, a minority group in Syria with strong ties to Shiite Islam—was rescued by the Iranians; by Hezbollah, the Lebanese militant group, which sent thousands of troops into Syria to bolster the regime; and, most recently, by Russia. Since September, when Russian President Vladimir Putin mobilized his forces in Syria, the Russians have carried out hundreds of airstrikes against rebel targets. Assad’s government, which was teetering, is safe for now.

Then came Turkey’s downing of the Russian fighter jet last month. Erdoğan, his Syria policy in a shambles, had picked a fight with his vastly more powerful neighbor. Putin, acting like the calculating former intelligence officer that he is, then unloaded a trove of intelligence that revealed the extent of Turkey’s official coöperation with ISIS oil smuggling. It’s been a bad a month for Erdoğan.

This brings us to the Turkish troops in Iraq. The focal point of the anti-ISIS campaign in Iraq is Mosul, the country’s second-largest city, which ISIS rolled into in July, 2014. Last month, Kurdish forces, backed by American airstrikes, cut the highway that connected Mosul to the ISIS base in Syria. There are still a few roads leading into Mosul that ISIS can use to resupply its fighters, but the Kurds are moving to cut them, too. Very soon, the ISIS fighters inside Mosul will be isolated. “If you cut those roads, the quality of life inside Mosul is going to deteriorate very quickly,’’ a former American intelligence official who works in the region told me.

There is no credible Iraqi force that can move into Mosul, kick out ISIS, and occupy the city, and there probably won’t be for a long time. But the siege of Mosul has begun. Clearly, Turkey, which has boxed itself out of a solution in Syria, wants to be a part of whatever happens in northern Iraq. The Turkish troops that rolled across the border last week took up positions in the town of Bashiqa, a few miles outside of Mosul. The public reason given by the Turks is that the troops were sent to bolster a contingent of forces that was already there to train a Sunni militia. But no one really believes that.

The Iraqi government may not have invited the Turks, but it seems pretty clear that the Kurdish Regional Government, which oversees the autonomous region of northern Iraq, did not protest when they arrived. In any case, the Iraqi Kurds can’t say no to the Turks: the thing that all Kurdish hopes of independence rest on is the oil pipeline that sends Kurdish oil to the Mediterranean every day. It runs through Turkey, and Erdoğan could turn it off at any moment if he wanted to. On Wednesday, the Kurdish leader Massoud Barzani, whom I Profiled in the magazine last year, met with Erdoğan in Ankara.

So, finally, why did the Turks go into northern Iraq? It seems pretty clear that Erdoğan, whose policy has failed in Syria, is trying to be relevant again. “Erdoğan wants to be part of whatever happens in Mosul, and putting troops there guarantees that,’’ a senior Iraqi official told me.

Is it going to work? Maybe. But the danger, increasingly, is that with so many major countries jockeying for power in Syria and Iraq events will spin out of control. The Russian cruise missiles flying over northern Iraq are just one example. Several have already crashed in northwestern Iran; just wait until that happens in Iraq.

Turkish troops in Iraq; Russians, Iranians, and Hezbollah fighters in Syria: the Middle East is a very busy place. The longer the war goes on in Syria, the greater the risk that it turns into something much worse.