Ms. 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 Mr. Erdogan took power. Even the most remote areas of the Istanbul metropolitan area showed a defeat for Mr. Erdogan’s mayoral candidate.

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

At a late-night news briefing in Istanbul, Mr. Imamoglu said he was trusting in Turkey’s institutions more than the AKP to confirm his victory. “I don’t expect this from the party,” he said. “For years to come AKP will not accept my win.”

Opposition mayors in Turkey’s two most important cities give the Republican People’s Party high-profile opportunities to show how it can govern effectively, with control of municipal services from garbage collection to mass transit. And Mr. Imamoglu has promised that as mayor of Istanbul, he would audit the books, a prospect that could create new problems for the AKP should he uncover evidence of corruption under the ruling party’s watch.

The Republican People’s Party, long criticized for a lack of organization, for once was well prepared for the election. Mr. Imamoglu, 49, a former district mayor, mobilized thousands of volunteers to observe 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,” Ms. Kocoglu said.

A decisive moment came at 9 p.m. Sunday when Mr. 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 favored by the government for results.