He accused the judiciary of "unconstitutional" behavior and said that as Iran's president he did not need permission to visit Evin prison, north of Tehran. The clash is part of an internal power struggle between Ahmadinejad and hardliners in Iran's political elite, and a sign of the increasing tensions between the two ahead of presidential elections scheduled for June.
Ahmadinejad, who cannot run for reelection because of term limits, once enjoyed the backing of the country's conservative clerical establishment but lost it when he was perceived to challenge the authority of Supreme Leader Ayatollah Ali Khamenei.
His policies have been challenged in parliament and some of his allies have been prosecuted, including his top press advisor Ali Akbar Javanfekr, who was jailed last month after being convicted of publishing material deemed insulting to Khamenei.
Iran's state prosecutor said Sunday that the judiciary rejected Ahmadinejad's request to visit Evin prison where Javanfekr is held, saying the president's planned visit appeared to be politically motivated. Gholam Hossein Mohseni Ejehi said in remarks published in several Iranian newspapers Monday that Ahmadinejad would do better to focus on Iran's deepening economic problems than to visit Evin.
Ejehi asked how is it that the president never asked to visit Evin in his seven years as president but wants to go there now that his close aide is behind bars.
Ahmadinejad's political opponents in the parliament and the judiciary claim that the president seeks to visit Evin in order to highlight the case of Javanfekr, who is serving a six-month prison term.
Ahmadinejad responded with a strongly-worded letter addressed to the head of Iran's judiciary, Ayatollah Sadeq Larijani, posted on the presidential website.
"I have to remind you that in the constitution, there is nothing that requires asking the permission or agreement of the judiciary when it comes to exercising the president's legal duties," Ahmadinejad wrote, according to the ISNA news agency.
Larijani wrote a confidential letter to Ahmadinejad Sunday rejecting the president's demand to visit Evin, but the president revealed the contents of the note Monday and there was nothing confidential when it comes to the rights of the nation.
"On the basis of what article of the Constitution can you (judiciary) make a political interpretation of an issue and prevent implementation of the constitution? Can you, as head of Judiciary, issue a verdict based on your political discretion?" Ahmadinejad asked.
He also vowed to visit the prison despite being blocked now.