The outcome of what may be the most divisive of the Islamic Republic's elections will determine where Iran, locked in a worsening confrontation with the West over its nuclear program, is headed.
Ahmadinejad, whose re-election for a second four-year term in 2009 triggered Iran's worst political upheaval since the 1979 Islamic Revolution, cannot run again but he's determined to install his closest associate and former vice president, Esfandiar Rahim Mashaei, or someone like him, as his successor.
Khamenei has immense political, religious and ideological authority as well as the support of the powerful Revolutionary Guards Corps.
Khamenei, a former president himself, was a revolutionary associate of the Islamic Republic's founder, the late Ayatollah Ruhollah Khomeini, and represents the hard-line theological Old Guard, the 1979 generation.
Ahmadinejad, a controversial populist who was once Khamenei's protege and leans toward the secular, is the brash nationalist challenger who wants to marginalize the mullahs who have run Iran since 1979.
This unprecedented power struggle erupted two years ago when Ahmadinejad turned against his former benefactor amid wide scale riots against his disputed re-election and the mass roundup of liberals, reformers and student protesters.
Khamenei overruled Ahmadinejad's appointment of key allies to important posts, such as intelligence minister, as the president increasingly and openly challenged the supreme leader's authority.
The Times of London reported Feb. 27 that Ahmadinejad is planning an "extraordinary attack" on the clerical establishment in a bid to "separate mosque and state."
Ahmadinejad and Mashaei, his chief of staff whose son is married to the president's daughter, want to "effectively dismantle the theocratic structure of Iranian government that has stood since the revolution," the Times reported.
But Ahmadinejad's already in a corner. All candidates for the June 14 polling must be vetted by the Guardian Council, a 12-member body with wide constitutional powers. It's unlikely to approve Mashaei, or anyone considered to be in Ahmadinejad's pocket, as a candidate.
The council consists of six clerics selected by the Supreme Leader, and six jurists chosen by the Majlis, or Parliament, where Khamenei has a firm majority.
A possible candidate from Khamenei's camp is Ali Larijani, a member of a powerful political family that's allied with the supreme leader, although he hasn't disclosed any intention to run for president.
He's a bitter opponent of Ahmadinejad and they have engaged in some pretty down and dirty exchanges of late that have stunned even scandal-hardened politicos in Tehran.
In one of the most bizarre mud-slinging episodes, Ahmadinejad created an uproar by publicly accusing Larijani's brother Fazel of corruption during a raucous parliamentary session. He played a videotape that purported to show Fazel Larijani promising a businessman to get him fat government contracts in exchange for $20 million.
Larijani responded by practically throwing Ahmadinejad out of the Majlis.
Larijani's elder brother, Sadeq, retaliated by having Said Mortazavi, a shadowy figure close to the president, arrested on a charge of entrapping the younger Larijani.
On Feb. 18, Iran's Supreme Court upheld death sentences for three businessmen and a banker in a high-profile, $2.8 billion corruption case linked to Ahmadinejad.
Sentencing four people to death in a single corruption case is unprecedented. It was seen as a not-too-subtle warning to Ahmadinejad that his allies and business cronies could face similar fates if they fund whoever runs as his candidate.
Another Larijani sibling, Ayatollah Sadegh Larijani, is head of the judiciary.
Iranian exile and analyst Amir Taheri described this high-profile scrapping as "Mafia-style squabbles."
And he observed that "while rival factions are engaged in a zoological struggle for power, the country" -- facing the specter of conflict with the United States and its sanctions-crippled economy collapsing -- "is looking increasingly like a rudderless ship adrift in stormy seas."
Khamenei has ordered mass arrests of reformists, even moderates, the largest roundup in years, as the power struggle intensifies.
A measure of the grim mood pervading Tehran is illustrated by a call by one of Khamenei's clerical representatives, Ali Saeedi, that the Revolutionary Guards should "engineer" the elections.