Turkey and Iran have been rivals throughout history, yet they have both learned how to manage that rivalry in order to prevent unnecessary clashes. The two neighbors have been quite careful in their bilateral relations, as they don't want to find themselves in a war against each other. That's why, despite all disagreements, both countries have never missed an opportunity to cooperate when they had common interests.
Both countries have been careful about the ongoing crises in Syria and Iraq, as well, because they didn't want to disrupt the existing balance between them. The actions of third parties, however, have changed the equation. Let's remember that Iran has always been close to Russia and Turkey is a U.S. ally. As long as the U.S. and Russia knew how to control their rivalry, things were relatively easy for decision makers in Iran or in Turkey.
Yet today, there are doubts about how the U.S. and Russia will be able to control their rivalry. Russia is quite active in the Middle East and it now wants to replace Iran's clout in Iraq and Syria by directly intervening in those theaters. Perhaps Russia has decided that Iran's influence over local actors is only exacerbating the "Sunni reaction," so it wants to smooth Iran's clout in the region. By the way, Russia is quite angry with the Iranians, as well, because of their current energy policy.
Russia insists on having a say in Iran's commercial relations and energy exports toward Europe and China. Russia's decision to get closer to Turkey and to launch new cooperation initiatives on energy is the result of Russia's new attitude toward the Iranians.
The U.S. has of course noticed Russia's resentment toward Iran and decided to benefit from it. Washington has thus started to send positive signals to Iran; like limiting Turkey's influence in the Middle East by using Kurdish groups. In the meantime, the U.S. has started to blame Saudi Arabia for having sponsored radical Sunni movements across the region.
So it is like the U.S. and Russia were swapping their allies: Washington is getting closer to Iran while Moscow is getting closer to Turkey. During this delicate process, Israel plays the role of the holder of the balance, assuring that the regional balance of power is maintained, preventing things from going out of control.
Nevertheless, this swap is only temporary and it is definitely not sustainable. What is essential is that the U.S. and Russia still have the intention to manage the Middle East together. They just don't know how.
In the meantime, Turkey and Iran share a common vision: they don't want an independent Kurdish state in the region. They also know they have to stand together to prevent it. The problem is, neither the U.S. or Israel will oppose if a Kurdish state emerges. Russia will be more reticent, though.
Therefore, the first thing Russia and the U.S. should agree on is whether or not Iraq and Syria will preserve their territorial integrity. If they do, then they will have to decide who should rule in Damascus and Bagdad. The U.S. and Russia will probably ask Iran and Turkey to give their idea on the future shape of Iraqi and Syrian governments, but they will have to keep Saudi Arabia and Qatar away from the negotiating table. If they cannot do that, the Sunni-Shiite divide will block every compromise attempt.
The diplomatic processes are continuing, and the battles on the ground, too. Involved countries use the war in order to strengthen their position at the negotiation table. This process will continue at least until we know who the next U.S. president will be.
It would be better, probably, if Turkey and Iran discuss the matter on a bilateral basis and develop a solution, which they will present to the Americans and Russians. Otherwise, we all risk being on the losing side.