ISTANBUL — President Recep Tayyip Erdogan’s party said Tuesday that it had submitted challenges to election results that showed its candidates had been defeated in Istanbul and Ankara, Turkey’s largest cities, in local elections two days earlier that dealt a rare setback to Erdogan at the ballot box.

The Justice and Development Party, or AKP, has repeatedly prevailed in elections since 2002 and was the leading vote-getter on Sunday. But its losses in major cities — including Istanbul, Turkey’s financial capital — were a significant symbolic defeat for Erdogan and threatened to weaken his powerful party machine, analysts said. 

The election came in the midst of an economic downturn that had focused voter anger on Erdogan’s handling of the crisis, analysts said. Urban voters may have also bristled at his caustic campaign rhetoric, they added, and his frequent attempts to link his political opponents to terrorism. 

The AKP challenged vote tallies in all of Istanbul’s 39 districts, where Ekrem Imamoglu, the mayoral candidate from the main opposition Republican People’s Party, or CHP, was leading by about 25,000 votes.

On Tuesday, AKP Deputy Chairman Ali Ihsan Yavuz asserted that the gap between Imamoglu and his opponent, Binali Yildirim, had narrowed to just over 20,000 votes. The gap would continue to close, he said, calling the elections “one of the biggest stains in our history.”

But the head of the Istanbul chapter of the CHP said it also had challenged results in 22 districts and expected its share of the vote to rise, according to the Reuters news agency.  

As the AKP plastered posters across Istanbul thanking the city in what was widely interpreted as a victory lap, Imamoglu called the spectacle “a shame” and suggested he would continue to fight. 

“My determination is just as high as my patience,” he told reporters, shortly before he traveled to Ankara, the capital, where he visited the tomb of Mustafa Kemal Ataturk, the founder of the modern Turkish state. Imamoglusigned a guest book there as “Mayor of Istanbul Metropolitan Municipality.”  

The AKP said it was also challenging results in Ankara that showed the CHP candidate there had prevailed. As tensions mounted, Erdogan — who kept a punishing schedule in the run-up to the election, giving multiple speeches every day — did not comment on the standoff.  

Ibrahim Kalin, a top adviser to Erdogan, on Tuesday dismissed speculation that the election setbacks signaled “the beginning of the end” of the president’s political dominance. The president’s party and its coalition partner had won a majority of the votes, Kalin said on Twitter, and Erdogan would not face another election until 2023.   

“They will never learn,” he wrote of the president’s critics.