Iranian President Mahmoud Ahmadinejad’s refusal to cut ties with his best friend and top adviser is damaging his presidency and could imperil his ability to keep his job, analysts here say.

What started as a disagreement between Ahmadinejad and the country’s supreme leader, Ayatollah Ali Khamenei, over the forced resignation of Iran’s intelligence minister last month has evolved into public attacks against the president and his adviser, Esfandiar Rahim Mashaei, by former supporters. The criticism has come from influential clerics, members of parliament and military commanders who are aligned with the supreme leader, creating the biggest rift in Iranian politics since Ahmadinejad’s disputed 2009 election victory.

Mashaei, 51, who has been at the center of a string of controversies in recent years, is proving to be one of Ahmadinejad’s biggest liabilities. In recent weeks, several of the adviser’s associates have been arrested, although the precise accusations remain unclear. Among those detained are the prayer leader at the presidential complex, the elderly wife of a prerevolutionary foreign minister, and a man who is said in Iranian news accounts to have engaged in sorcery.

State media now refer to Mashaei as the “leader of a deviated current.” According to the semiofficial Fars News Agency, Mashaei and his allies are seeking to diminish the role of clerics in the Islamic republic and are predicting the imminent arrival of the Shiite messiah, known as the Mahdi, in order to attract supporters. “This group is unprecedented in the history of the Islamic Republic,” the managing director of Fars, Hamid Reza Moghaddam-Far, wrote on Wednesday.

Iranian media outlets have reported corruption allegations against Mashaei. In addition, parliament has stopped funding for his council for Iranians living abroad, with some accusing the group of contacting the Iranian opposition in the West.

Supporters of Mashaei say that his name is being tarnished in order to pressure the president. Ahmadinejad himself has shown no sign of giving in. “They are making up these jokes of fortunetelling and exorcism,” Ahmadinejad said in an appearance on state television. “Our rivals clearly want to make the people laugh.”

The reason Ahmadinejad has stuck by Mashaei is simple, analysts say: They need each other.

“Both men are two pieces of the same puzzle,” said Amir Mohebbian, an analyst who has in the past supported the president’s policies. “They don’t contradict each other, they complete each other.” He said that the arrest of Mashaei was a real possibility but that it would lead to “an aggressive” reaction by Ahmadinejad.

Ahmadinejad on Sunday publicly declared his allegiance to the supreme leader, complaining that “enemies” were trying to create a rift between the two men. Mashaei, speaking for the first time since the latest controversy erupted, on Friday told the Islamic Republic News Agency that some within the government were “raising the issue of a rift in order to meet their own petty group and factional objectives.” He also expressed strong support for Khamenei and emphasized that the “recent propaganda” had “no effect whatsoever” on the president.

But few doubt that the relationship between the president and the supreme leader — which was once a close alliance — has become deeply strained.

Despite the conflict between the president and his former supporters, analysts say there is little chance Ahmadinejad will drop his trusted aide, who is known to be his main strategist and was expected to try to succeed the president in elections slated for 2013. “If Mashaei is pushed out, Ahmadinejad will no longer be the president as we know him,” said Abbas Abdi, an analyst who is critical of the government’s policies. “Retreating on this issue means total defeat for Ahmadinejad.”

But analysts say the president’s continued support for Mashaei carries its own risks.

“A simple cost-and-benefit calculation shows that Ahmadinejad’s behavior is politically not explainable,” Mohebbian said. “These men feel they are on some kind of divine mission, although nobody knows what that mission is.”

The controversies surrounding Mashaei, a tall and slender former intelligence official, began several years after Ahmadinejad’s rise to power in 2005.

Ahmadinejad had carefully crafted an image of caring for the poor, promoting Islam and battling injustice, making him popular among the country’s lower classes. Mashaei, by contrast, struck up friendships with famous actresses, advocated that Iranian pop singers residing in Los Angeles return to the Islamic republic and asserted that God is dependent on humans — the opposite of traditional Shiite Islamic teaching.

Some of Mashaei’s remarks and actions have broken major religious and ideological taboos in Iran. In 2008, he shocked clerics by opening an official meeting with women playing musical instruments and carrying in the Koran. Also that year, Mashaei said that Iran has no problems with the people of Israel, its archenemy, which led to a public rebuke by Khamenei, the supreme leader.

Mashaei has made overtures to other regional rivals, including Jordan, in moves that raised the ire of many in the clerical and security establishment. But his views on the West, and on the United States, appear to mirror those of other top Iranian officials: He said in an interview with the New Yorker magazine that U.S. officials are “the biggest liars in the world.”

Last year, Mashaei said Iranian culture was more important than Islamic culture. Angered clerics called for his resignation, saying that Mashaei was tapping into the nationalistic feelings of Iranians at the expense of Islam.

On Wednesday, a powerful clerical council, led by an official once known to be one of the president’s key supporters, rebuked Ahmadinejad over his decision on Monday to suddenly merge eight key ministries without asking parliament for its consent. “He is losing all his key supporters,” Mohebbian said.

But although Ahmadinejad is increasingly isolated, he must not be underestimated, analysts say.

“He is the president. His decisions can seriously frustrate the supreme leader’s policies,” said one political analyst, who spoke on the condition of anonymity to discuss a sensitive issue. “If he resigns, the Islamic republic will enter uncharted territory.”