ISTANBUL — Turkey’s electoral authorities wiped away a crushing defeat for President Recep Tayyip Erdogan on Monday, ordering a rerun of the race for mayor of Istanbul won by the opposition and heightening the prospect of social unrest and a new economic crisis.

The decision by the High Election Council was immediately condemned by the opposition party as a capitulation to Mr. Erdogan and a blow to the democratic foundations of the country, which have drifted closer to authoritarianism under his 18 years in power.

Mr. Erdogan has jailed journalists, isolated adversaries and conducted mass purges of the police, the military and the courts. He has strengthened his powers under the Constitution. Now, critics say Mr. Erdogan has managed to invalidate what had been a humiliating defeat for his party in Istanbul, the country’s largest city, commercial capital — and the president’s hometown.

Though Mr. Erdogan secured another five-year term as president with sweeping new powers in an election last year, he was rendered suddenly vulnerable by his party’s poor showing in the March 31 voting, which took place as the economy has begun to falter.