Although the incumbent president, Hassan Rouhani, still has a strong lead in Iran's presidential elections, the latest opinion polls reveal that his main rival, Ebrahim Raisi, is narrowing the gap.

Raisi, the preferred candidate of Iran's Revolutionary Guards Corps (IRGC), is a former member of Iran's judiciary. He also is the guardian of the shrine of Imam Reza in the holy city of Mashhad - a role he was appointed to by Supreme Leader Ayatollah Ali Khamenei. IRGC's second choice for president, Mohammad Bagher Ghalibaf, the sitting mayor of Tehran, has withdrawn from the race to consolidate the hardline vote.

What started as an electoral race between hardliner candidates and moderate/reformist candidates supporting Rouhani soon turned into a major battleground.

The three live pre-election TV debates were fairly lacklustre at first, but accusations and counter accusations of corruption and embezzlement soon revealed a divide deeper than ever before in the Islamic Republic; a divide that was accentuated by the supreme leader taking the side of the hardliners.

Images and videos of the banned reformist leader, former President Mohammad Khatami, were shared on social media websites with his message of support for Rouhani.

OPINION: Are elections in Iran an exercise in futility?

The popularity of these videos and images revealed that the reformist movement was still alive despite years of repression and hundreds of supporters in jail. Images of quintessentially modern Iranian women in green headbands demanding freedom and justice, which were common ground before the 2009 elections, reappeared - this time accompanied by Rouhani supporters in purple headbands and scarves with a message to vote for Rouhani. Famous actresses such as Baran Kosari and Taraneh Alidoosty as well as iconic sports personalities also joined in.

Standing up to the supreme leader

Rouhani continued to challenge the establishment in all his rally speeches. He even made a reference to IRGC saying he has respect for the organisation but it should remain within its own area of remit; indirectly asking IRGC not to interfere.

"People would reject a government of executions and prison", Rouhani said in an indirect reference to his rival Raisi, who as a leading prosecutor is accused of approving the execution of thousands of activists during the 1980's, and the imprisonment of pro-democracy protestors during the 2009 presidential elections.

Behind this glow of revival, however, Rouhani and the reform movement were taking a serious and potentially dangerous political risk. They were going against the rulings of the supreme leader who had silenced Khatami. They could be reprimanded.

OPINION: Iran's presidential election puts populism to the test

Recently Khamenei vowed to punish "anyone who violates the national security". But Rouhani was undeterred. "We say hello to freedom, hello to the Leader, hello to reform, we say hello to Said Mohammad Khatami", he said to cheering crowds.

Khatami, who still has a large following among reformists, broke his silence and went against the supreme leader's ruling by asking his supporters to vote for Rouhani. In a video released on his official Twitter account, he defended Rouhani's record and the success of the nuclear deal he made with the P5+1 group, saying the lifting of sanctions had helped Iran's economy.

The supreme leader vs the president

The supreme leader began criticising Rouhani's economic record a few weeks ahead of the elections. He advocated a resistance economy, more centralised and based on increasing internal production and lowering imports; a line that could be argued suits the IRGC which has a monopoly over Iran's economy.

"Candidates should promise not to look outside the borders", Khamenei said in a reference to Rouhani's attempts at increasing Western investment in Iran.

A few days later the supreme leader rejected Rouhani's claim that the nuclear deal had forestalled a possible US attack."What has kept the war away, is the [anti-US] stand of the people" Khamenei tweeted.

He also criticised the government for promoting the United Nations 2030 education plan, describing it as a "Western-influenced" agenda. The plan is part of the UN 2030 agenda for sustainable development, which Rouhani signed on to.

"In this country, the basis is Islam and the Quran," Khamenei said. "It makes no sense to accept such a document in the Islamic Republic". Khamenei later ordered the agreement be put on hold.

OPINION: Iran's election is not about moderates or hardliners

Rouhani defended his record at all his rallies, claiming the economy had improved after the nuclear deal and vowing to lift the remaining sanctions.

Raisi, on the other hand, held rallies in poorer areas and accused Rouhani of failing to bring any tangible benefits to the man in the street who was struggling with unemployment and poverty. The IRGC and Ghalibaf used their vast logistical network to support him.

The endgame

The supreme leader's interventions seem to have caused the opposite to what he might have intended: a shift from the centre - not towards the hardliners but towards the reform movement.

President Rouhani, a centrist and a man of the establishment, has never sounded more reformist. On top of directly hailing Khatami in his rallies, he used the reform leader's image in his official electoral TV clips. And, as if that were not enough, he included two former reform candidates, Mehdi Karoubi and Mir-Hossein Mousavi - who have been under house arrest since 2009 - in his campaign clips. All three are accused of "sedition" and "cooperation with external powers" and have been disowned by the supreme leader.

Rouhani knew the clips would be censored but he nevertheless used them to give a clear message to the supreme leader that he could be heading for another crisis if he goes against him.

The absence of the powerful Ayatollah Hashemi Rafsanjani does not seem to have reduced Rouhani's popularity. Rafsanjani passed away last year and it was believed that his intervention had brought Rouhani to power in 2013. Other influential centrists such as Ali Larijani, speaker of Parliament, and Ali-Akbar Nateq Nouri, a former favourite of Khamenei, continued to express their support for Rouhani.

Yet Rouhani may face a tougher race than expected. A pre-election national poll showed that 42 percent of those interviewed saw unemployment as the main problem facing the country. That may mean that a significant section of Iran's 80 million population has not benefited from Rouhani's outreach to the world. So Raisi may make inroads in the poorer areas, where he has focused his campaign.

The 68-year old incumbent president has risked his political career by confronting the supreme leader and the IRGC. He has taken this risk in an effort to win the centrist, moderate, and reformist votes, as well as youth and women's votes.

In a year of regional turmoil and uncertain relations with the new US administration, the elections on May 19 are of crucial importance both for the future status of the supreme leader and the survival of President Rouhani and the reform movement. The result may prove to be a more serious challenge than the establishment can muster.

Dr Massoumeh Torfeh is the former director of strategic communication at the UN Assistance Mission in Afghanistan and is currently a Research Associate at the London School of Economics and Political Science, specialising in Iran, Afghanistan and Central Asia.

The views expressed in this article are the author's own and do not necessarily reflect Al Jazeera's editorial policy.