Turkish President Recep Tayyip Erdogan in Istanbul on Sunday. (Bulent Kilic/AFP/Getty Images)

TURKISH PRESIDENT Recep Tayyip Erdogan turned Sunday’s local elections into a referendum on his increasingly authoritarian leadership. He feverishly barnstormed the country, attempting to rally his base with claims that Turkey’s existence as a nation was at stake. He even played videos of the recent massacre of Muslim worshipers in New Zealand. Fortunately, the result was a decisive rebuff. According to results reported Monday, Mr. Erdogan’s party lost the mayorships of Turkey’s two most important cities, Ankara and Istanbul, for the first time in a quarter-century, along with city halls in almost every other large city.

Mr. Erdogan’s own position is unaffected: His term in the powerful presidential post has four more years to run, and his coalition still has a majority in the national parliament. But the defeat of his hand-picked nominees in the big cities, driven by a massive turnout of voters, sent an unmistakable message that Turks are tiring of his strongman style — and that the country’s democracy, while on life support, still breathes.

Much will now depend on how Mr. Erdogan, who has grown increasingly intolerant during 15 years in power, responds to the stinging defeat. He could try to overturn the results, particularly in Istanbul; party leaders have asked for a reexamination of votes set aside as invalid. He could also use his powers to oust the elected mayors, as he has previously done in dozens of municipalities in Kurdish-populated areas. The wiser course would be to pragmatically address the issues that lifted the opposition, including an economy that has slipped into recession — and repression of the media and civil society.

Not only domestic policy is at stake. The Trump administration has been quietly waiting for the end of the election season in the hope that Mr. Erdogan will be more open to resolving conflicts that threaten to rend the U.S.-Turkish alliance. Chief among them is Turkey’s imminent acquisition of an advanced missile system from Russia that could subject it to tough sanctions, including the cancellation of a deal to acquire U.S. F-35 warplanes. There is also the unresolved conflict over Syria, where Mr. Erdogan is threatening to attack U.S.-backed Kurdish forces.

History is not encouraging. Following his defeat in a 2015 parliamentary election, Mr. Erdogan launched a military campaign against Kurds in southeastern Turkey, accelerating his descent into authoritarianism. If he now refuses to compromise over the Russian missiles or the Syrian Kurds, the Trump administration will have little choice but to treat him as an adversary. That could compound the damage to the Turkish economy, which began to slide after President Trump imposed tariffs and other sanctions last year in response to Turkey’s imprisonment of an American pastor.

As in the case of domestic affairs, it is in Mr. Erdogan’s interest to respond pragmatically. The Trump administration should provide him with incentives to do so — but be prepared to stand firm if he again lashes out.