Mahmoud Ahmadinejad, Iran's irascible, unpredictable but devout president, may be forced to resign in the coming weeks as a political crisis far greater than the massive street violence which followed his re-election in 2009 threatens to overwhelm him and his court favourites in the government.

The overweening influence of his close friend and confidant Esfandiar Rahim-Mashaee, the president's chief of staff – who is blamed for the firing of two intelligence ministers and for infuriating even the Supreme Leader, Ayatollah Sayed Ali Khamenei – is expected to bring down Ahmadinejad in one of the most spectacular putsches in the history of the Islamic Republic.

Iranian politicians are already speculating on who will succeed the president – Ali Akbar Salehi, the foreign minister and for four years the head of Iran's atomic agency, is a favourite – as three of Rahim-Mashaee's close allies have been purged in just three days over the past week, arrested by security agencies while Ahmadinejad has remained uncharacteristically silent. Mohamed Sharif Malekzadeh, who served briefly as Ahmadinejad's foreign minister; Ali Asghar Parhizkar, director of the Arvand free trade zone in the south of Iran; and his opposite number in the Aras trade zone in the north, Ali-Reza Moqimi, have all been charged with corruption, a dangerous accusation in the Islamic Republic where a fine line separates "corruption on earth" from "an enemy of God".

The darkness may soon close in over Rahim-Mashaee – the nearest Iran has to a government spin doctor – and thus embrace the man over whom he has allegedly cast a spell, Ahmadinejad himself.

The potential fall of Ahmadinejad is a story worthy of any Persian tale, a pageant of court favouritism, abrupt firings of otherwise loyal ministers, apparent challenges to the Islamic heritage of Iran, and an acute case of political hubris by the president himself, all overshadowed by the immense power of Ayatollah Khamenei who holds the near-divine role created by the Republic's founder, Ayatollah Ruhollah Khomeini.

Some compare Ahmadinejad's predicament to that of Ayatollah Sayed Hussein Ali Montazeri, the man Khomeini chose as his successor as Supreme Leader – and then abruptly dismissed when Montazeri's son-in-law was seen to have too much power over him. In Ahmadinejad's case, the danger is Rahim-Mashaee, a civil engineer from the Caspian region of Iran, whose daughter is married to Ahmadinejad's son and who packed the president's office with his own supporters from the same region of Iran. At one point Ahmadinejad wanted to make him first vice-president – a post equivalent to any other nation's prime minister – only to be thwarted by his opponents.

According to those who have followed the saga from Ahmadinejad's original election to the presidency in 2005 – he only won in the second round and was never expected to hold office – the conservative clergy and revolutionary founders, the "principalists" as they call themselves, believed that Ahmadinejad never had the stature for the role as Iran's political leader.

But the new president maintained his popular support by touring hundreds of villages, small towns and cities, from Isfahan and Mashad to Tabriz, in order to create the profile of a "people's representative" rather than that of a distant father-figure. "It was like a US president heading off to town hall meetings in New Hampshire every week," one of his supporters told me.

When Mir Hossein Moussavi stood against Ahmadinejad in the 2009 election – which the former believed he would have won had the votes been counted fairly – he would appear on the streets of Tehran with educated, T-shirted young men and women with their hair showing beneath their scarves on one pavement, while bearded men and women in chadors would curse him from the other. Another of the president's supporters said: "I voted for Ahmadinejad because he stood up for the underclass – unlike Moussavi's comrade Mohamed Khatemi [who was president for eight years before Ahmadinejad] who always seemed to be drinking English tea with Jack Straw."

But then Rahim-Mashaee appeared in the president's office as head of Iran's heritage organisation, becoming ever closer to Ahmadinejad. Rahim-Mashaee had been in the Sepah, the Revolutionary Guards, during the 1980-88 war with Iraq and had then become a radio manager for Iran's state broadcasting company; rumour has it that he also worked in the intelligence ministry. "But by the end of Ahmadinejad's first term, Rahim-Mashaee was in the president's office all the time," an Ahmadinejad detractor said.

"He was saying things that the conservatives, the principalists, didn't like – he said that Iran's conflict was with Israel, not with the Israeli people. But the conservatives said that these 'people' were occupying the land of Palestine. When he appeared to be putting Iran's heritage in front of the country's Islamic heritage, he annoyed the clergy in Qom. He made speeches as long as Fidel Castro. I've been to them. He wasn't careful with money on his heritage projects. There were allegations of financial mismanagement; that he didn't keep good records."

When Ahmadinejad "won" the 2009 election, Ayatollah Khamenei said the poll was fair, but then Ahmadinejad said he wanted Rahim-Mashaee to be vice-president – even before he announced his cabinet. He was persuaded to drop the idea.

During his first term, he had appointed Akbar Ajaii as his minister of intelligence; he stayed for almost all of Ahmadinejad's first four-year term but constantly criticised Rahim-Mashaee's influence over the president. Then just two weeks be

fore the 2009 election, Ahmadinejad fired him. This was a deliberate insult: he could have waited another two weeks and then dropped Ajaii.

Another opponent remarked: "People began to ask Ahmadinejad to get rid of Rahim Mashaee; even the 'marja', the highest Shia religious authority, asked him to. We thought Ahmadinejad would give way. But he didn't. Anyone in the cabinet who criticised Rahim-Mashaee got fired. They would criticise him one day – and the next day they didn't have a job. So in Iran today, some people say that Rahim-Mashaee has cast some kind of spell over Ahmadinejad – that he has captured Ahmadinejad's mind. It's unusual for anyone to resist all this pressure."

In Iran, a president is expected to share power – on the principle that your enemies will increase if you do not do so. The latter is exactly what happened to Ahmadinejad. Ayatollah Khamenei went so far as to write a personal letter to the president, saying that the appointment of Rahim Mashaee as first vice-president "will upset many of your supporters", reminding the president that "this is for your own good". He reluctantly accepted that the post of first vice-president was lost – but then made Rahim-Mashaee his personal chief of staff, effectively elevating him to the third most important man in the Islamic Republic after Khamenei and himself.

Khamenei continued to publically support the president, but was said to be deeply angered by his behaviour. More trouble arose when Ahmadinejad decided to fire his new intelligence minister, Heydar Moslehi, less than two months ago after he too, criticised Rahim Mashaee. Infuriated, Ayatollah Khamenei wrote another letter, this time directly to Moslehi, saying that he "needs to continue in his post". His dismissal was never officially revealed and Moslehi has been re-instated.

Slighted, Ahmadinejad stalked off home in a sulk, refusing to attend cabinet meetings and boycotting his own duties as president for a week. Then, suddenly he was back, full of praise for the Supreme Leader and the system of "velayat-e faqih" under which Khamenei held his own post, repeating that he "followed the Supreme Leader" in all his decisions. There was no more talk of Rahim-Mashaee, whose own compatriots are now being hastily arrested.

In Iran, they say that Ahmadinejad, who under the Iranian constitution cannot stand for president again in 2013, has at last realised his own desperate situation. In Tehran, Rahim-Mashaee is now daily being denounced as a "deviant" – another dangerous expression in the lexicon of the Islamic Republic – and the president is supposedly prepared at last to sacrifice his chief adviser to save his own skin. The fact that a senior official in the government was prepared this week, privately, to predict to me his possible demise, suggests that it may be too late.

Possible Successors

Ali Akbar Salehi

Iran's Foreign Minister and former envoy to the UN nuclear watchdog is believed to be a favourite to succeed President Ahmadinejad.

Mohammed Sharif Malekzadeh

Previously viewed as one of Ahmadinejad's closest allies, Iran's former deputy foreign minister was forced to resign after only a few days in the job before being arrested on corruption charges last week.

Esfandiar Rahim-Mashaee

Ahmadinejad's chief-of-staff is not only one of the President's closest political advisers, his daughter is also married to the Iranian leader's son. A leaked US cable made public in April added fuel to speculation that Ahmadinejad favours Mashaee as his successor.

Ali Asghar Parhizkar

Head of the Arvand free trade zone in southern Iran, Parhizkar was also arrested on charges of corruption last week.

Ali-Reza Moqimi