Supporters of Iranian President Hassan Rouhani at a rally in Tehran on April, 29. (Vahid Salemi/Associated Press)

A former Iranian president banned by his country’s judiciary from speaking publicly defied the restrictions to endorse President Hassan Rouhani for a second term, warning voters that Iran faces international isolation if a hard-line opponent is elected to power this month.

Mohammad Khatami, a pro-reform leader who served two terms as president from 1997 to 2005, is under a domestic media ban, and the local press is prohibited from publishing his image or mentioning his name.

But the former president announced on his website Tuesday that he would support Rouhani, who is also a moderate, for reelection on May 19. His endorsement could help mobilize turnout for the incumbent, who is facing tough competition as he struggles to defend his record on the economy and respond to questions about the benefits of a nuclear deal struck in 2015 with world powers, including the United States.

Iran is in the middle of a campaign blitz featuring two main challengers to Rouhani. No Iranian president has failed to secure a second term since shortly after the 1979 revolution.

“Rouhani is our candidate,” Khatami said in his statement Tuesday. If the president is defeated, he added, it would increase the “possibility of a return to international isolation and sanctions.”

Rouhani is facing five opponents: Ebrahim Raisi, a powerful cleric; Mohammad Bagher Ghalibaf, the hard-line mayor of Tehran; Mostafa Mirsalim, a conservative former culture minister; Vice President Eshaq ­Jahangiri, a moderate and reformist; and former vice president Mostafa Hashemitaba, also a reformist.

Raisi, who is in charge of Iran’s holiest shrine in the city of Mashhad, was widely seen as the favorite of conservatives. But his poor performance in a televised debate last week could undercut his candidacy, boosting Ghalibaf. The conservative mayor lost to Rouhani in 2013, but he has tapped into discontent among ordinary Iranians over the slow-growing economy.

Ghalibaf “went after Rouhani hard on unemployment, where the president is indeed vulnerable,” Cliff Kupchan, chairman of the political risk firm Eurasia Group, wrote in a briefing note on the debate this week.

If either Raisi or Ghalibaf wins the presidency, “the impact would be significant,” Kupchan wrote, echoing Khatami’s assertion that Iran’s relations with the rest of the world could suffer.

Both candidates are close to the Islamic Revolutionary Guard Corps, Iran’s most powerful security institution. It also has far-reaching economic ties.

“Raisi would make little or no effort to attract foreign investment,” Kupchan wrote, adding that members of the Revolutionary Guard “as a general matter benefit from a closed market.”

During his tenure as president, Khatami, 73, encouraged Iran’s rapprochement with the West. And last year, he released a video in support of moderate and reformist candidates running in parliamentary elections. He is widely credited with helping them sweep to power.

But his support for reformist candidates has landed him in hot water with powerful conservatives, including in the judiciary. His website was blocked — an extraordinary move by the government against a former president.

The bonds between Khatami and Rouhani have strengthened since mass protests over the disputed reelection of conservative president Mahmoud Ahmadinejad in 2009.

In 2013, Khatami joined another former president, Ali Akbar Hashemi Rafsanjani, in publicly backing Rouhani — propelling him to the fore in a field of better-known moderates. At the time, Khatami called Rouhani “my esteemed brother.”

Last year, Rouhani also defied the ban on mentioning Khatami by suggesting that he drew inspiration from the former president.

“No one can silence those who served the nation,” Rouhani told a crowd in Khatami’s home city of Yazd in central Iran.

Khatami and Rouhani have clear limits on how far they can push Western engagement and other reforms in a country where all key policy decisions must be cleared by the supreme leader, Ayatollah Ali Khamenei, who usually sides with hard-liners.

During his 2013 campaign, Rouhani promised to lift restrictions on social media, among other freedoms. But Iranian journalists and other activists remain in jail, and key leaders are still silenced.

Meanwhile, there are splinters within pro-reform groups, as Rouhani has been unable to win the release from house arrest of top leaders of the Green Movement protests of 2009: Mehdi Karroubi and Mir Hossein Mousavi and his wife, Zahra Rahnavard. Karroubi and Mousavi were candidates in the presidential race and joined the post-election protests.

Murphy reported from Washington.