Mr. Karroubi works from a villa on a quiet street in Tehran that ends at a rundown palace once occupied by Shah Mohammed Reza Pahlavi. It is one of the many symbols of his standing among the revolutionary elite. He was jailed nine times by the shah and spent years in prison, where he grew close to inmates of widely different political persuasions: nationalist, socialist, Islamist, said Rasool Nafisi, an Iran expert based in Virginia.

Image Mehdi Karroubi, an opposition candidate, in Tehran at a June protest of the disputed election. Credit... Olivier Laban-Mattei/Agence France-Presse — Getty Images

“These forced companionships, Karroubi wrote in his autobiography, made him aware of the pain of the others, and relieved him from sectarian behavior,” Mr. Nafisi said.

After the overthrow of the shah, Ayatollah Khomeini put Mr. Karroubi in charge of the Imam Khomeini Relief Committee and the Martyrs Foundation, two of the nation’s most important and wealthiest institutions. He also served twice as speaker of Parliament, where he earned a reputation as a conciliator; served on the powerful Expediency Council; and was appointed adviser to the subsequent supreme leader, Ayatollah Ali Khamenei.

So it was hard for the leadership to brand him an enemy of the state when he posted on his Web site last month an impassioned, unyielding and damning letter to the nation, written in response to the judicial finding that his allegations of the rape of imprisoned protesters were unfounded.

“The ugliness has reached the point that instead of the perpetrators and propagators and people behind this oppression, it is Mehdi Karroubi whom they want to put on trial,” he wrote. “I take refuge with you, oh God, from these catastrophes which some are causing and are not only a disgrace to the Islamic republic, but a disgrace to Iran.”

Mr. Karroubi’s disenchantment with the revolution he helped create began not with the elections in June, but with the balloting that brought Mr. Ahmadinejad to office four years ago. Mr. Karroubi was a candidate then, too, and late into the night after the polls closed, he was running second, behind Mr. Rafsanjani.