The Iranian president has been weakened not only by his direct challenge to Ayatollah Khamenei, but because of the long-standing theological challenge he and his allies have posed to the ruling group of clerics.
Ahmadinejad challenged Iran's clerics - and lost
The top commander of Iran's Islamic Revolutionary Guards Corps delivered an explosive speech last week, suggesting that "internal evils" within Iran have been "organised to defeat the revolution". He warned that the political figures who represent "internal evils" violated the constitution - an offence that carries a potential jail term. Finally, General Mohammed Ali Jafari vowed to defeat them and protect the system.
At first glance, this seems like the standard rhetoric of the Islamic Republic of Iran's conservative and hard-line camp, usually aimed at opposition leaders from the Green Movement, who are currently in jail or under house arrest. But look again: Gen Jafari is not pointing the finger at opposition leaders, but directly at President Mahmoud Ahmadinejad and his allies.
What we are witnessing in Iran today is a seismic political moment. A power struggle has emerged between the hard-line Mr Ahmadinejad and his allies and the hard-line Supreme Leader Ayatollah Ali Khamenei and his more formidable allies. It has rocked the political establishment, and tested the survival skills of Mr Ahmadinejad, who has faced a withering political assault over the last two weeks.
Indeed, Gen Jafari was polite by comparison to some of the charges against Mr Ahmadinejad. A senior cleric, once close to Mr Ahmadinejad, called him a liar; another cleric implied that Mr Ahmadinejad and his allies are engaged in acts against God. Some members of Parliament have suggested that his position means nothing unless he obeys the Supreme Leader. Hard-line newspapers have questioned his leadership skills.
Dozens of the top aides of Mr Ahmadinejad's allies have been jailed, including the cleric who handles Friday prayers at the presidential compound. Other clerics close to Mr Ahmadinejad's top ally, Rahim Mashaei, have been jailed on charges of practising "sorcery" and conjuring up djinns, or evil spirits.
For Mr Ahmadinejad, who landed in Istanbul for an international conference on Monday, the episode has damaged his reputation among the conservative power base that rules Iran today. He has emerged weakened and battered, but not yet beaten.
Mr Ahmadinejad has also learnt a hard truth of the Islamic Republic: while the shifting contest of power and influence among political elites can be fluid and dynamic, there are certain red lines that cannot be crossed. Mr Ahmadinejad crossed two of those lines over the past year.
The first red line is the supremacy of Ayatollah Khamenei. Iran's political system, the velayat-e-faqih, grants overwhelming power to a single cleric ruler, or faqih. This Supreme Leader, as he is often called, has virtual veto power over all matters of state. In theory, the Supreme Leader's role is to stand above the system as a neutral arbiter. In practice, Iran's two Supreme Leaders - Ayatollah Ruhollah Khomeini (1979-88) and Ayatollah Khamenei (1989-present) - have regularly imposed their will on internal politics and make all the key foreign policy decisions.
The immediate source of this dispute was Mr Ahmadinejad's dismissal of the intelligence minister, Heydar Moslehi. When Ayatollah Khamenei demanded that the minister be reinstated, Mr Ahmadinejad sulked and stayed away from cabinet meetings for 11 days, essentially forcing a choice between Mr Moslehi and himself. Ayatollah Khamenei did not blink and eventually Mr Ahmadinejad, under withering criticism, went back to work.
But it was not the first time that Mr Ahmadinejad defied the Supreme Leader. He has dismissed several others of the Supreme Leader's allies from influential positions, including Ali Larijani, the current Speaker of Parliament. And when Ayatollah Khamenei objected to the appointment of the controversial Mr Mashaei as the first vice president in 2005, Mr Ahmadinejad withdrew the nomination but appointed Mr Mashaei as his chief of staff. Mr Mashaei is a polarising figure who has demonstrated an anti-clerical strain and supports more social freedoms; he is also related to Mr Ahmadinejad through marriage of their children.
The Moslehi affair seemed to be the last straw, but beyond the political struggle with the Supreme Leader, Mr Ahmadinejad has also crossed another red line, one that might even be more consequential. He and his allies have trafficked in a school of thought that diminishes the influence and power of the clergy. Mr Ahmadinejad has repeatedly invoked the Hidden Imam, Shi'a Islam's messiah figure who, according to belief, went into hiding more than 1,100 years ago and will return to bring justice to the world.
Mr Ahmadinejad claims to have a direct line to the Hidden Imam. In a sense, he has superseded the clerical intermediary role. Traditional conservative clerics have been uncomfortable with this sort of talk, but a documentary film produced by an Ahmadinejad ally, suggesting that the Imam's return is imminent owing in part to Mr Ahmadinejad's presidency, rocked the clerical establishment.
It is also worth noting that Mr Moslehi is the last cleric standing in Mr Ahmadinejad's cabinet. While Iran's secular middle class largely distrust the president, many are also strongly anti-clerical. The clerical establishment knows this, but generally dismisses the middle class as too "westoxicated". On the other hand, it's very dangerous when a figure like Mr Ahmadinejad, a traditional conservative with a social constituency similar to that of the hard-line clergy, is seen as anti-clerical.
As for Ayatollah Khamenei, he has emerged as the temporary victor in this power struggle, but his own reputation suffered after the 2009 post-election crisis in which he called Mr Ahmadinejad's victory "a divine assessment" and endorsed a heavy-handed crackdown on protesters.
Perhaps the real winner from this hard-line melee is the Iranian Revolutionary Guard. Some IRGC elements have benefited immensely from Mr Ahmadinejad's presidency, especially in business where they have been awarded billions of dollars in contracts. Their leadership has also demonstrated their "king-maker" role by coming to the aid of Ayatollah Khamenei, and essentially ending the dispute.
We have not heard the last of Mr Ahmadinejad, but he will return from Istanbul a smaller president, on a shorter rein, but probably with a few more fights still in him.
Afshin Molavi is a fellow at the New America Foundation and the author of The Soul of Iran