Iraqi rapid-response forces shout slogans during battle with Islamic State militants. (Thaier Al-Sudani/Reuters)

A STRING of bombings in Baghdad and a gunman’s bloody rampage in an Istanbul nightclub over New Year’s weekend underlined the continuing capacity of the Islamic State to strike beyond the borders of its shrinking territory. In the past two years, the terrorist movement has lost some 50,000 fighters, according to U.S. estimates, as well as more than half of the ground it once controlled in Syria and Iraq. But with offensives to capture its two biggest remaining cities, Mosul and Raqqa, stalled or moving slowly, the Islamic State retains the potential to inflict grievous harm on the countries around it, as well as to target Western cities.

In particular, there is reason to worry that Iraq and Turkey, the targets of the weekend attacks, are in danger of effectively losing their war with the terrorists. Both dispatched their armies to capture Islamic State territories last year and recorded significant gains. But both are at risk of political, economic and social breakdown as a result of terrorist counterattacks and of their own counterproductive measures.

Iraqi counterterrorism units advancing through Mosul, and the U.S. advisers and air power backing them, deserve credit for tactics aimed at protecting civilians: The humanitarian cost of the 2½-month-old battle has been small compared with the assault on Aleppo by Syrian, Russian and Iranian forces. But the Iraqi troops have taken heavy casualties and, having aimed for victory by the end of 2016, the government of Prime Minister Haider al-Abadi says it may take months more.

Mr. Abadi’s government, meanwhile, is not delivering on his frequent promises to promote political reconciliation among Iraq’s sectarian factions. On the contrary, the parliament in Baghdad recently took measures that further alienated the Shiite-led government from Sunni and Kurdish leaders. The danger is that even after the recapture of Mosul, the country’s sectarian warfare will continue and perhaps even intensify as the factions compete for control over liberated territories.

Turkey appears at risk of its own meltdown in spite of — and because of — the authoritarianism of President Recep Tayyip Erdogan. Mr. Erdogan, who avoided action against the Islamic State for several years, dispatched troops to Syria in 2016, but he appears more intent on preventing the consolidation of a Kurdish-controlled region there than on helping to capture Raqqa. His brutal crackdown on Kurds and other political opponents inside Turkey has polarized the country, driving a wedge between groups that should be united against the terrorists. Some liberal Turks pointed out that the assault early Sunday on Istanbul’s Reina nightclub, a secular and cosmopolitan refuge, came after government religious officials criticized New Year’s Eve celebrations.

It remains likely that 2017 will see the elimination of the Islamic State’s self-declared caliphate as a territorial entity. But what may be more important to the security of the Middle East as well as the West is whether Turkey and Iraq are further destabilized. That will depend in part on whether the Islamic State can continue to mount devastating terrorist attacks like those of last weekend. The decisive factor, however, will be whether Mr. Erdogan and Mr. Abadi are able to embrace more constructive domestic policies.