Turkey's ties with Washington have been strained by US support for Kurdish fighters in Syria, and the visit by Iranian General Mohammad Bagheri is the latest sign that Ankara is increasing cooperation with other powers such as Iran and Russia.
Bagheri met his Turkish counterpart on Tuesday and Turkey's Defense Minister Nurettin Canikli on Wednesday in what Turkish media said was the first visit by an Iranian chief of staff since Iran's 1979 Islamic revolution. He also met with President Tayyip Erdogan.
Turkey and Iran have supported rival sides in Syria's six-year-old conflict, with Iran-backed fighters helping President Bashar Assad to drive back rebels battling to overthrow him, including some supported by Ankara.
Turkey is concerned that the Syrian chaos has empowered Kurdish forces who it says are closely tied to the long-running insurgency in its southeastern regions, as well as Islamic State fighters who have waged attacks inside Turkey, and is working with Iran and Russia to reduce the fighting in some areas.
An Iranian source said Bagheri was accompanied by the head of the ground forces of the Revolutionary Guards Corps, Iran's most powerful security entity.
"There have been no such visits between the two countries for a long time, but considering regional developments and security issues—border security and the fight against terrorism—there was a need for such a visit," Bagheri told Iranian state television on arrival on Tuesday.
The Iranian source said that, in addition to the war in Syria, the two sides would discuss the conflict in Iraq as well as dealing with Kurdish militants in the Turkish-Iranian border region, where Turkish media say Turkey has started building a frontier wall.
Russian military chiefTurkey, Iran and Russia agreed in May to set up "de-escalation zones" in Syria to try to stem the fighting in some parts of the country, including the northern province of Idlib, which borders Turkey and has since been overrun by jihadists linked to a former al Qaeda affiliate.
That has thrown into question any suggestion that the three countries could deploy a force to police the Idlib region.
"The negotiations regarding the Idlib issue are still ongoing," Turkey's Foreign Minister Mevlut Cavusoglu told Turkish broadcaster TRT Haber on Wednesday.
"After the Iranian chief of staff, the Russian chief of staff will also come to Turkey," he added.
Turkey has said for months that it is close to buying an S-400 missile defense system from Russia, and Erdogan said in July that the deal had already been signed.
Cavusoglu said Russia understood Turkey's sensitivities about arming Kurdish fighters better than the United States, although he said US officials had informed Turkey that the most recent shipments to the YPG did not include guns.
"The United States gives us reports about how many weapons they have given to the YPG every month," he said. The latest "said they gave armored vehicles and a bulldozer, but no guns."
Turkey's stepped-up military talks with Iran and Russia coincide with a major oil and gas deal involving firms from the three countries.
The Turkish firm Unit International said this week it has signed a $7 billion agreement with Russia's state-owned Zarubezhneft and Iran's Ghadir Investment Holding to drill for oil and natural gas in Iran.
Turkey is also discussing transporting more goods through Iran to the Gulf state of Qatar, which is locked in a dispute with its neighbors Saudi Arabia, Bahrain and the United Arab Emirates.