Istanbul - Step by step over the years, President Recep Tayyip Erdogan of Turkey sought to ensure nobody could challenge him. He marginalised adversaries. He purged the army, the police and the courts. He cowed the press. He strengthened his powers in the constitution. And he promised Turks a bright economic future.

So it was a huge surprise when the outcome of weekend municipal voting showed on Monday that Erdogan’s party had not only lost control of Ankara, the political centre, but maybe Istanbul, the country’s commercial centre, his home city and long-standing core of support.

Even if the results were not final, they amounted to the most momentous political earthquake to shake Erdogan in nearly two decades of basically uncontested control at the helm of Turkey, a Nato ally and critical linchpin of stability in the region.

What was different this time was the rapidly tanking economy and a highly disciplined opposition.

It deployed monitors to not only scrutinise the vote tallies but also sleep on sacks of sealed counted ballots to guard against possible tampering by members of Erdogan’s ruling Justice and Development Party, the AKP.

“We think they were not able to rig the election,” said Ilayda Kocoglu, 28, vice president of the Istanbul branch of the opposition Republican People’s Party, or CHP, who slept on some sacks herself. “They were not expecting us to be that organised, or that resolved.”

The results do not mean that Erdogan, whose term as president lasts for four more years, will change his behaviour. But the election showed Erdogan has weaknesses.

“It’s a catastrophe for him,” said Asli Aydintasbas, a senior fellow at the European Council on Foreign Relations. “We now know he is not invincible.”

Turkey’s weakened economy, which had rapidly expanded for years under Erdogan, was at the top of voters’ concerns, despite Erdogan’s exhortations that the problems are not of his making. The country tumbled into a recession in March. Unemployment exceeds 10 per cent, and up to 30 per cent among young people. The Turkish lira lost 28 per cent of its value in 2018 and continues to weaken. Inflation has reached 20 per cent.

Kocoglu said she and her colleagues understood within an hour of the closing of polls Sunday night that they were watching Turkey’s most momentous change since Erdogan took power. Even the most remote areas of the Istanbul metropolitan area showed a defeat for Erdogan’s mayoral candidate.

As of Monday night, results from the High Election Council had still not been fully released and Erdogan’s party had not conceded defeat in Istanbul. But the tally showed the opposition candidate, Ekrem Imamoglu, ahead with 99 per cent of the votes counted.

The Republican People’s Party, long criticised for a lack of organisation, for once was well prepared. Imamoglu, 49, a former district mayor, mobilised thousands of volunteers to observe the election at every ballot box in the greater metropolitan area and record the count on a specially designed application, giving the party its own independent tabulation.

“We were able to compare our numbers with theirs,” Kocoglu said.

A decisive moment came at 9pm Sunday when Erdogan gave his first speech of the evening, claiming victory for the AKP overall in the municipal district elections. The election commission suddenly stopped releasing election results for Istanbul, as did the semiofficial Anadolu news agency, which is widely followed on election nights as the source favoured by the government for results.

The president’s camp had already seen which way the vote was going and had stopped the count, said Ozgur Unluhisarcikli, the Ankara director for the German Marshall Fund of the United States, a research institute. “They stopped it to think what they could do,” he said. There was even discussion of some kind of intervention, he said.

“We were very afraid,” Kocoglu said. She recalled several elections when the election commission had stopped releasing details in the count, only to resume later and show an unlikely jump in favour of the ruling party.

By 11pm Sunday, Erdogan appeared in Ankara and conceded the loss of at least one city to the opposition - a huge setback by itself.

Then people inside Erdogan’s party headquarters posted screen shots taken from AKP computer monitors showing the opposition candidate in Istanbul leading the race. Erdogan’s own supporters were apparently leaking the information, said Aydintasbas at the European Council for Foreign Relations.

Erdogan has become increasingly aloof over the years, surrounded in his vast presidential palace by a smaller and smaller circle of aides and ministers.

Yet political analysts were in little doubt that he made the decision to allow the true results for the Istanbul vote to be released Monday morning in order to protect his own electoral legacy.

“Erdogan is not a crazy person; he is intelligent,” Kocoglu said.

Aydintasbas said Erdogan, who has always drawn legitimacy from the ballot box, would have seen it was impossible to alter the result. “There was no real way,” she said. “They did not find a way of doing it without losing legitimacy.”

Others said Erdogan knew the risks to the economy if there were a wrangle over the election or a protracted dispute. The lira could fall sharply, as it did last summer, with drastic repercussions for businesses and livelihoods.

By accepting the election result, Erdogan has saved the reputation of the Turkish electoral system, which has given him legitimacy over the last 17 years, Unluhisarcikli said. “With this one election Erdogan has dispensed the clouds of doubt of all the past elections.”