Iranian President Mahmoud Ahmadinejad’s announcement last week that he would pardon two jailed American hikers, only to be overruled the next day by his own judiciary, has caused him international embarrassment at a time when he is seeking better relations with the West.

But Iranian political analysts say the episode is a carefully planned political move aimed at helping Ahmadinejad domestically by exposing his rivals in the Islamic republic’s clerical establishment as stubborn and reactionary — while portraying him as reasonable.

“Ahmadinejad is trying to change the perception the middle classes have of him inside Iran,” said Amir Mohebbian, a political strategist who has good relations with Iran’s leaders. “He’s telling them: ‘I am not radical like the clerics. I am like you.’ ”

The controversy over the hikers was the dominant backdrop as Ahmadinejad headed to New York on Monday to participate in the annual United Nations General Assembly meeting at U.N. headquarters, where he is scheduled to speak Thursday.

Ahmadinejad told representatives of The Washington Post and NBC News last week that Shane Bauer and Joshua Fattal, two 29-year-olds who have been jailed in Iran for two years on espionage charges, would receive a “unilateral pardon” and would be home “within days.”

The next day, Iran’s judiciary, which is led by Shiite Muslim clerics who once supported Ahmadinejad but now oppose him, reacted angrily, stressing that Ahmadinejad did not have the authority to free the men. The judiciary postponed the release, demanding $1 million in bail. On Sunday, the men’s release was delayed again because one of the judges whose signature is required on the bail paperwork is on vacation.

Underscoring its unwillingness to compromise, the judiciary also refused to allow a delegation of U.S. religious leaders and American Muslims to visit Bauer and Fattal, even though Ahmadinejad had invited them to come to Iran to help return the two to the United States.

The delegation included Cardinal Theodore E. McCarrick, archbishop emeritus of Washington; the Right Rev. John Bryson Chane, Episcopal bishop of Washington; Nihad Awad, national executive director of the Washington-based Council on American-Islamic Relations (CAIR); and CAIR’s chairman, former North Carolina state senator Larry Shaw.

The group left Iran on Monday morning after six days of sightseeing and meetings with religious and political leaders, but without obtaining access to Bauer and Fattal. The two were arrested with a third American — Sarah Shourd, who was released on medical grounds last year — as they were hiking near the border between Iraq and Iran in July 2009. Bauer and Fattal were convicted of spying last month in a closed trial and sentenced to eight years in prison. Their families and the U.S. government have called the espionage charges bogus.

The struggle over the hikers’ case underscores Ahmadinejad’s increasing status as a rebel within Iran’s leadership. His new role escalated publicly in April when he clashed with Iran’s supreme leader, Ayatollah Ali Khamenei, over the forced resignation of a key minister. Since then he has been embroiled in public fights with the country’s ruling clerics, who say that Ahmadinejad and his inner circle of advisers are plotting to undermine their influence.

Ahmadinejad’s call for the hikers’ release is part of a broader effort to reinvent his domestic image as a reformer who dares to take on the Islamic republic’s highest leaders, analysts said. By doing so, Ahmadinejad hopes to attract support from middle-class Iranians — from bus drivers to doctors — who are demanding more freedoms, better relations with the world and less state interference in their private lives.

Ahmadinejad has been actively reinforcing his new image by publicly questioning some of the Islamic republic’s most important principles, including opposing the strict enforcement of the Islamic head scarf that women are required to wear. He has spoken out against a pet project of Iran’s clerics, the segregation of male and female students in universities, and he has continued to stand by his most controversial adviser, Esfandiar Rahim Mashaei. Hard-line clerics have called Mashaei a “sorcerer, Zionist and Freemason” because — among other things — he has said that Iranian nationalism is more important than Shiite Islam.

The parliamentary elections scheduled for March 2012 are seen as a rehearsal for a presidential election a year later, and Ahmadinejad is widely expected to push Mashaei as his successor. Ahmadinejad won reelection in a disputed vote in 2009 and cannot run again under Iran’s two-term presidential limit.

But attracting the middle-class vote on behalf of a chosen successor will be a challenge for Ahmadinejad. After his 2009 reelection, middle-class Iranians joined unprecedented street protests alleging election fraud, and they later turned against clerical leaders. Dozens of protesters were killed and thousands were arrested.

“Now Ahmadinejad needs their votes,” said Mohebbian, who does not support the president. “So he is trying to show that he will defend their rights internationally, but is not afraid to stand against the supreme leader.”

For those inside Iran hoping for detente with the world, the release of Bauer and Fattal will be welcomed as a positive step, although one unlikely to reverse their opposition to Ahmadinejad, analysts said.

“Yes, he is reaching out to the middle classes by such moves,” said Sadegh Zibakalam, a political scientist connected to Tehran University. “But it’s useless. They don’t have faith in Ahmadinejad anymore.”