The graffiti appeared in Istanbul’s Gayrettepe neighbourhood one morning last month. It showed the word “justice” written on a piece of paper held up by a disembodied hand. With a lighter, the other hand was setting fire to it.

“Write down ‘justice’ on a bit of paper. And then burn its corners,” the tag read. Within a day it had vanished, painted over by a municipality cleaning team. For the Istanbul street artist who goes by the name of Pepe, work is a constant game of cat-and-mouse with authorities seeking to remove his political creations.

“I drew that when the AKP (Turkey’s ruling Justice and Development party) cancelled the Istanbul election,” he said, sitting at a bar on Friday, two days before the city goes back to the polls in a controversial rerun today.

“We have a right to free and fair information that Turkish media suppresses these days. They do a good job of shutting down the opposition. But they’ll never completely manage it.”

That cat-and-mouse struggle, writ large, has been playing out across Istanbul this weekend as the AKP seeks to undo the mayoral victory of opposition coalition candidate Ekrem İmamoğlu. In the March election the previously unknown figure delivered one of the biggest challenges to President Recep Tayyip Erdoğan’s grip on Turkey in years. Although the governing AKP and nationalist party coalition secured more than 50% of the vote across the country in the 31 March polls, voters delivered an unexpected slap to Erdoğan’s party in four major cities, widely viewed as a rebuke for his handling of Turkey’s burgeoning financial crisis. Istanbul, governed by the AKP and its Islamist party predecessors for 25 years, was the most painful loss. The city constitutes one third of the entire Turkish economy and is the main driver of the AKP’s patronage networks. The president, who began his political career here as mayor in the 1990s and often talks about his deep love for his hometown, also viewed it as a personal rejection.

While critics say Erdoğan’s expanded presidential powers are helping erode Turkey’s democratic structures, in March voter turnout – traditionally high across the country – still stood at 84%. On election night many Istanbullus who had dejectedly shown up at polling stations to carry out their democratic duty earlier in the day watched the live results at first with curiosity, and then excitement. When the live stream of results from the government news agency stopped at about 9pm, people across the country realised İmamoğlu, the People’s Republican party (CHP) candidate, was pulling ahead of his AKP rival, powerful and well-funded former prime minister Binali Yildirim.

Facebook Twitter Pinterest Supporters gather at a rally for the pro-government candidate, in the Kagithane district of the city. Photograph: Anadolu Agency/Getty Images

Initial results gave İmamoğlu a razor-thin lead of 0.28% with 99.8% of ballots counted, but the city was forced to endure more than two weeks of waiting for recounts demanded by the AKP before İmamoğlu was finally given an official mandate to start work at city hall.

Rerunning elections and removing elected officials from their posts have become routine in the 16 years since the AKP came to office. But even by Turkish standards, this race is different. It eventually dawned on the president that conceding Istanbul would accelerate the already shifting dynamics within his party and threaten his position. As Erdoğan famously once said: “Whoever loses Istanbul, loses Turkey.”

“The stakes are very high,” said Sinem Adar, a researcher at Berlin’s Humboldt University. “Less obvious, [than the AKP’s financial considerations] but perhaps the more important implication of the rerun is for the post-election power configuration between Erdoğan and his allies. Cracks within the party that have also become quite visible since [the elections] are further contributing to weakening his grasp of power.”

Five weeks after the election, Turkey’s electoral board upheld one of the dozens of complaints made by the AKP and declared the race for mayor of Istanbul must be rerun, triggering an outcry from opposition parties and even from members of the AKP itself, who worried the rerun would dent the party’s democratic credentials.

The decision has, curiously, turned İmamoğlu into both victor and victim, now fighting on a platform of saving Turkish democracy. The softly spoken 49-year-old has become a household name across Turkey, welcomed as a breath of political fresh air and touted as a possible presidential candidate in 2023.

In inclusive, conciliatory speeches – the opposite of those of the firebrand politicians who have come to dominate Turkey’s polarised political scene – İmamoğlu talked about the need for the city to come together to overcome problems with unemployment and the rising cost of living.

He won hearts and minds in pious and working-class AKP neighbourhoods all over the city by showing up to meetings and reaching out both by knocking on doors and through social media across Istanbul’s ethnic, political and religious divides. Yildirim, in contrast, was almost completely sidelined by the president himself, who held huge rallies where he framed the local elections as a matter of “national survival” and accused opposition parties of links to terrorism.

Despite almost blanket pro-AKP media coverage, İmamoğlu’s campaign cut through, reinvigorating a beleaguered and poorly organised CHP unable to field candidates with broad cross-party appeal in the past.

In an interview shortly after he entered city hall, İmamoğlu told the Observer he was unperturbed by the prospect of an electoral rerun. “I am not feeling any pressure,” he said. “There are 16 million people in this city waiting for me to serve them and do the job I was elected to do.” But tensions across the city have been steadily building ahead of Sunday’s vote. Many in Istanbul were keen to show their support after the election results were cancelled on 6 May. After a child shouted “Her şey çok güzel olacak” – “Everything will be alright” – at İmamoğlu’s tour bus, the slogan became the new rallying cry for the level-headed candidate, chanted in bars and at football matches and shared millions of times as a hashtag on social media. Pepe, the graffiti artist, made it the basis of one of his latest pieces.

Facebook Twitter Pinterest ‘Pepe’ standing in front of some of his work, outside his fathers bar in Nevizade, Istanbul, Turkey. Photograph: Bradley Secker/The Observer

The AKP has learned from İmamoğlu’s success, copying some of the CHP candidate’s ideas, such as subsidised public transport and making an effort to reach young people, conservative-leaning Kurdish voters and AKP supporters who did not vote last time. The president has largely been absent. “I have every faith Binali [Yildirim] will win,” said Melike, 38, a municipality worker, shopping with her teenage children as pro-AKP songs blared from a party stand in Taksim Square.

“Last time people wanted to punish the government for their failings, I understand that. But they must know what is at risk now. The party has heard their message very clearly and knows it must deliver now.”

Despite the best efforts of Yildirim’s team, according to more reliable polls, two days ahead of the rerun İmamoğlu’s lead widened to about 8-9% – meaning the AKP is facing the humiliating prospect of losing again.

Facebook Twitter Pinterest ‘Write down “justice” on a bit of paper. Then burn it at a corner’. Graffiti by Pepe was quickly painted over. Photograph: Instagram/@Iremkomitt

In the past few days, the usually solid AKP election platform has wobbled. Erdoğan has lashed out at İmamoğlu, hinting he is in league with the outlawed Kurdish People’s Workers party (PKK), and a bizarre last-minute tactic – a message from jailed Kurdish PKK leader Abdullah Öcalan urging potential swing-voter Kurds not to vote for the CHP – backfired after Kurdish political parties reiterated their support for İmamoğlu. “Conservative Kurds, people who used to vote for the AKP, have woken up,” said Hassan Kaplan, 27, one of Istanbul’s estimated 4 million Kurds. “I’m scared there is too much expectation on İmamoğlu, but he’s the only person who has given us hope in a while.”

Neither AKP nor CHP supporters know what to expect when Sunday’s results come in or what course of action the president might take if faced with another unsatisfactory result. Street protests are rare in Istanbul these days, quickly broken up by police – but any fresh attempt to overturn a second İmamoğlu victory could trigger unrest in new and unexpected parts of the city.

Everyone in Istanbul knows what happens in the vote will have profound implications for the city and the country. On a wall in the shopping district of Osmanbey, the faces of Pepe’s “Her şey çok güzel olacak” mural smile at passers-by. For now, at least, it has not been taken down. “It doesn’t matter if they do,” Pepe said. “The city is fired up. Even if they steal the election again, the cracks in their armour are there. We know that now.”

Additional reporting: Gokce Saracoglu