When Mahmoud Ahmadinejad became Iran's president seven years ago, he made his presence felt through incendiary statements targeted at Israel and the west. Now, in his dying days in office, he is going out all guns firing. Only this time he is taking aim at the edifice of the political system that championed him.

This week, while schools across Iran were made to decorate classrooms as part of the celebrations marking the 34th anniversary of the 1979 revolution, Tehran was the scene of an unprecedented, and very public, spat at the highest levels of the Islamic republic. On Sunday, a power struggle between Ahmadinejad and the speaker of the parliament, Ali Larijani, reached a climax when the former, in a dramatic sequence of events, played a secretly filmed tape for jeering MPs that showed the latter's brother, Fazel, allegedly trading on his sibling's influence for financial gain in a conversation with Saeed Mortazavi, the caretaker of Iran's social welfare organisation.

Ahmadinejad was addressing the parliament in defence of his employment and welfare minister, Abdolreza Sheikholeslami, who was facing impeachment for appointing Mortazavi, a close ally of the president, to the welfare job. Mortazavi, a former judge and prosecutor general of Tehran, is loathed by the MPs because of his links to the death in custody of at least three protesters in the aftermath of the 2009 disputed presidential election which gave Ahmadinejad a second term.

"These are audio and video, and the tape is clear," said Ahmadinejad, in quotes carried by the semi-official Isna news agency. "If the honourable parliament speaker sees fit, we can turn over the 24 to 25 hours of recordings to you." A few minutes of a barely audible tape were played as millions of Iranians listened to the extraordinary parliamentary session live on national radio.

"It was a good thing that you showed this to let people learn about you character," Ali Larijani retorted.

Larijani's brother, Fazel, denied the accusations and said he would sue Ahmadinejad and Mortazavi.

On Monday, the fallout was felt when Tehran prosecutor's the office announced Mortazavi, known by activists as "butcher of the press" and "serial human rights abuser", had been arrested. While prosecutor general, he tightened control of the press, closing 18 newspapers within two days in 2000, and handed down lengthy jail sentences to protesters and activists. He was also linked to the case of Zahra Kazemi, an Iranian-Canadian photographer who died while in Evin prison in 2003.

Ahmadinejad, however, was infuriated by Mortazavi's arrest and labelled the judiciary under Larijani a "family institution". On Tuesday, pro-Ahmadinejad supporters were reported by the conservative Javan Online site to have gathered in front of the judiciary's office in Tehran, protesting against the arrest.

Before leaving Tehran on a historic visit to Egypt, Iran's first president to go there since 1979, Ahmadinejad said: "The judiciary should be the judiciary of the nation and not one special family's private institute."

The official departure ceremony in Tehran was not attended by any representatives from Ayatollah Ali Khamenei's office. Analysts say Ahmadinejad's revelations in parliament were directed at Iran's supreme leader, who cannot be publicly criticised by officials and is known for intransigence.

Hossein Bastani, at BBC Persian, wrote that Ahmadinejad was sending a message to Khamenei that if he refuses to intervene on the president's behalf whenever he is at odds with his rivals, the ayatollah should expect "irretrievable blows" to the Islamic republic.

In delivering his inflammatory speech, the president was defying Khamenei who a few weeks ago warned officials against bickering, saying those who bring disputes to public attention are "betraying" the revolution.

Iranian parliamentarians have previously attempted a number of times to impeach Ahmadinejad but their latest bid in November was blocked when Khamenei came to the president's rescue, saying that such a move would play into the hands of Iran's enemies. The ayatollah, who has the final say on all state matters, is an erstwhile patron of Ahmadinejad and despite several attempts by the latter to defy him publicly, is believed to prefer a smooth departure for the lame-duck president on face-saving grounds.

But as the presidential vote in June approaches, Ahmadinejad has shown increasing signs of defiance and disobedience as he struggles to preserve his dwindling power. Under Iranian law, he cannot run for a third term.

Larijani, meanwhile, is enjoying a great deal of influence and is seen as a potential frontrunner in the election.