Former Iranian president Mahmoud Ahmadinejad appears at the Interior Ministry's election headquarters in Tehran on April 12, 2017, as candidates register to run in the upcoming presidential election.  / AFP PHOTO / ATTA KENARE/AFP/Getty Images

When Mahmoud Ahmadinejad put his hat into Iran's presidential ring a couple of weeks ago, onlookers literally gasped.

The hard-liner had, of course, already run the country for eight years, as an isolationist with a talent for riling the West. His flawed reelection in 2009 sparked the Green Movement, which brought thousands of protesters into the streets. In the ensuing government crackdown, hundreds of people were arrested. The government said at least 27 were killed, but rights groups put the death toll higher.

It was such a fraught time, and Ahmadinejad was so contentious, that the country's supreme leader urged him to stay out of politics. "I told him he should not participate in that matter," Ayatollah Ali Khamenei said last year, according to his official website. It’s "not in his interest and that of the country." (Other former presidents have received similar advice. The media is banned from using the name or image of Mohammad Khatami, a reformist who led the country between 1997 and 2005. Former president Ali Akbar Hashemi Rafsanjani was barred from seeking another term in 2013. Iran’s first post-revolutionary president, Abol Hassan Bani-Sadr, lives in exile in France.)

When asked by reporters, Ahmadinejad brushed off the ayatollah's words as advice, not instruction. He has been campaigning around the country, and even joined Twitter.

Turns out, he was wrong.

This week, the country's Guardian Council announced the candidates for president in the May 19 election. About 1,600 men and women had applied to run; the final list includes six names. As was widely expected, incumbent Hassan Rouhani will seek reelection. Other contenders include Ebrahim Raisi, a hard-line cleric who is close to Khamenei; Tehran Mayor Mohammad Bagher Ghalibaf, a conservative former military officer; and Eshaq Jahangiri, a first vice president in Rouhani's government.

Ahmadinejad's name was nowhere to be seen.

On social media, Ali Akbar Javanfekr, an Ahmadinejad ally, tried to downplay the decision, writing that Ahmadinejad registered to run only out of “national, religious and revolutionary duty.” Referring to Ahmadinejad and his former vice president, who was also turned down, Javanfekr wrote: “Thank god, the Guardian Council removed the duty from their shoulders."

Others didn't take it as well. "Disqualifying candidates is illegal. If Ahmadinejad has committed a crime, why hasn’t been put on trial all these years?” tweeted Iranian user @sahartwitt. “We are opposed to the disqualifications of the Guardian Council, it’s not right for the council to decide what the people are meant to decide."

That anger may be just what Ahmadinejad intended. Like supporters of populists around the world, his base is already suspicious of the political establishment. The supreme leader's decision riled Ahmadinejad's base and burnished his image as an opponent of the establishment.

Experts say the presidential election is likely to be a tight three-way contest between the conservatives Raisi and Ghalibaf, along with Rouhani. Raisi, in particular, is a prominent Shiite cleric who is being touted as a possible successor to the supreme leader. (Jahangiri has already said that he will drop out of the race eventually — that he is only running to defend Rouhani's accomplishments when he appears in television commercials and in debates.)

Raisi and Ghalibaf belong to the same conservative coalition. If they both stay in the race, they may split the conservative vote and assure Rouhani's victory, particularly if the incumbent's moderate-reformist coalition holds. That's a big if, of course, especially in Iran.