Ahmadinejad (left) and his close ally Hamid Baghaei, right, shake hands after registering their candidacy for the upcoming presidential elections on April 12. ─ AP
Khamenei had advised Ahmadinejad not to run, and his attempt to become a candidate was widely seen as a public snub to the Supreme Leader, which is nearly unheard of in the Islamic Republic.
The disqualification of Ahmadinejad, a two-term president, draws attention to the criteria that the Guardian Council, the governmental body which vets candidates, uses in the selection process.
Khamenei appoints half of the members of the Guardian Council, and by disqualifying Ahmadinejad, the body runs the risk of being seen as a rubber stamp for the Supreme Leader, who is the highest authority in the country.
Rouhani and Raisi will likely face off over the economy as well as the nuclear deal signed with Western powers, which Rouhani has highlighted as his signature achievement during the past four years in office.
Iran agreed to curb portions of its nuclear program in exchange for the lifting of several sanctions as part of the deal.
Political hardliners see the deal as a form of capitulation and are wary of the opening it presents for Western companies to work in the Islamic Republic.
In recent days, Raisi, who was appointed by Khamenei as the head of a multi-billion-dollar religious foundation last year, has repeatedly blasted Rouhani's economic performance.
Khamenei has also criticised Rouhani's economic performance in recent speeches and called on the government to do more to address the issue of unemployment.
About 3.2 million Iranians are jobless out of a total population of 80 million.
Rouhani has claimed that inflation has decreased and the economy has grown on his watch. He has also said in recent public appearances that the judiciary and security forces should be more mindful of ordinary Iranians' rights and privacy, which is likely to appeal to his supporters.
Rouhani was elected in 2013 with a promise to bring about greater individual freedom and detente with the West. Some of his supporters say he has fallen short of those goals.
Regardless of how the rivalry between Rouhani and Raisi plays out, top Iranian officials, including Khamenei, have said they will confront any individuals or groups who attempt to destabilise the country. Iranian police fanned out across Tehran after the names of the candidates were announced on Thursday night, according to the news site of the Iranian judiciary Mizan Online.
Live debates between presidential candidates, a feature in the last two presidential elections, have also been called off, according to state media. Pre-taped interviews will air instead, perhaps to keep candidates from stoking up their supporters.
Widespread protests broke out and continued for months after the disputed election of Ahmadinejad in 2009, and led to dozens of deaths and hundreds of arrests.
“The bitter incidents of (2009) will not be repeated,” Tehran prosecutor Abbas Jafari Dolatabadi said on Wednesday, according to state media.