Threatened by Islamist militants, Iraq and Turkey say they will improve intelligence and security cooperation.

Iraq and Turkey said on Thursday they would improve intelligence and security cooperation in order to battle the Islamist militant group that calls itself the Islamic State. The announcement marks a turnaround in relations between two countries that were at each other’s throats just a year ago.

Iraqi prime minister Haider al-Abadi told a news conference in Baghdad the countries had agreed to “exchange information and have full security cooperation.” He added, “The Turkish prime minister also wants us to have military cooperation in the face of terrorism and Daesh and we welcome that,” using the Arabic acronym for the Islamic State group.

Ahmet Davutoğlu, Turkey’s former foreign minister and the architect of its failed “zero problems with neighbors” policy, said the agreement marked “a new page in relations between Turkey and Iraq and that is why I hope that there will be close cooperation between our security and intelligence agencies to defeat terrorism.”

Before the Islamic State threatened both Iraq and Turkey, tensions between the countries had run high. Turkey traded oil with Iraq’s autonomous Kurdish province over objections from Baghdad, prompting Abadi’s predecessor, Nouri al-Maliki, to brand Turkey a “hostile state.” Turkish leader Recep Tayyip Erdoğan, who was elected president in August, accused Maliki in turn of exacerbating the sectarian divides in his country between Shia and Sunni Arabs and Kurds.

Maliki’s Shia-dominated administration largely sidelined Sunnis who had been in power during the dictatorship of Saddam Hussein. Desperate about their prospects, many Sunnis supported, or at least acquiesced in, the Islamic State’s uprising.

In June, the group — an Al Qaeda offshoot — proclaimed a caliphate in northwestern Iraq as well as in the northeast of Syria where it battles the regime of President Bashar al-Assad.

Turkey opposed Assad when Maliki cautioned against outside intervention to remove him, warning it would lead to even worse sectarian violence. Assad’s opponents are mostly Sunni as well.

Turkey has been reluctant to involve itself in Syria’s civil war, however, or in the fight against the Islamic State there. Perhaps the most effective opponents of both the Assad regime and the jihadists have been the Kurds — who seek autonomy in, or possibly independence from, Turkey as well.

Kurdish militants in the north of Syria are affiliated with the Kurdistan Workers’ Party, or PPK, which the Turks and their NATO allies consider to be a terrorist organization.

Under Maliki, Turkey improved relations with the Kurdistan Regional Government in the north of Iraq, led by President Masoud Barzani’s Kurdistan Democratic Party. Although it too advocates Kurdish independence, it is more pragmatic and focused on building state institutions south of the Turkish border.

Were the Turks to join the United States and Arab Gulf powers in fighting the Islamic State — President Barack Obama announced earlier this month he would send another 1,500 American troops to Iraq to help its army combat the group — it would effectively side with the very Kurdish “terrorists” it is fighting within its own borders.