The busiest square in Tehran is dominated by an enormous billboard with a drawing of a young man in the uniform of the Revolutionary Guards Corps, extending his hand to invite Iranians to follow his path. Underneath the image, teenagers line up, flashing victory signs, as they take selfies with the placard in the background.

 

In life, the man on the billboard, 26-year-old Mohsen Hojaji, was just as anonymous as the thousands of other Iranians who have rotated in and out of war zones in Iraq and Syria in recent years. But after having been taken prisoner, videotaped and later beheaded by the Islamic State in August, Mr. Hojaji has been transformed by Iran’s government into a war hero, the face of a new surge in Iranian nationalism.

 

After years of cynicism, sneering or simply tuning out all things political, Iran’s urban middle classes have been swept up in a wave of nationalist fervor.

 

The changing attitude, while some years in the making, can be attributed to two related factors: the election of President Trump and the growing competition with Saudi Arabia, Iran’s sectarian rival, for regional dominance.

 

Iranians listened during the 2016 campaign as Mr. Trump denounced the Iran nuclear treaty as “the worst deal ever negotiated” and promised to tear it up. They watched in horror when, as president, he sold more than $100 billion worth of weapons to the kingdom of Saudi Arabia and participated in a traditional war dance in Riyadh. And they are alarmed at the foreign policy moves of the young Saudi crown prince, Mohammed bin Salman, whom they see as hotheaded and inexperienced.

 

At the same time, they now believe they have something to be proud of, with Iranian-led militias playing a central role in defeating the Islamic State militant group in Syria and Iraq, increasing Iran’s regional influence in the process.

 

The two most popular stars in Iran today — a country with thriving film, theater and music industries — are not actors or singers but two establishment figures: Gen. Qassim Suleimani, the leader of Iran’s regional military effort, which is widely seen as a smashing success; and the foreign minister, Mohammad Javad Zarif, the symbol of a reasonable and measured Iran.

 

In short, it appears that Mr. Trump and the Saudis have helped the government achieve what years of repression could never accomplish: widespread public support for the hard-line view that the United States and Riyadh cannot be trusted and that Iran is now a strong and capable state capable of staring down its enemies.

 

On the day of a state-orchestrated commemoration ceremony for Mr. Hojaji, Morteza Hosseinzadeh, a 33-year-old graduate of theater studies at Tehran University who considers himself a reformist, came out early to pay his respects. Dressed in black and holding a poster bearing the portrait of the new martyr, he looked every bit like the hard-line supporters of the clerical government.

 

“There are many here like me, who don’t care for the Islamic Republic and its rules,” he said. “But today is about something bigger than that, one of us has been killed. At the same time this American president is breaking our hearts with his rhetoric and threats. We have to choose sides. I choose for my country.”

 

Iran’s hard-liners are savoring the reversal in their fortunes, after losing influence in the Obama years. “Thanks to Trump’s dishonest, cheating and crazy remarks, he has proved what we have said for a long time: America cannot be trusted,” said Hamidreza Taraghi, a hard-line political analyst. “Many didn’t believe us, but now they do.”

 

The seeds of the new nationalism were planted with the election of the moderate Hassan Rouhani as president in 2013. It was the first glimmer of hope for Iran’s urban middle classes since the brutal crackdown on street protests following the 2009 presidential election, widely seen as rigged.

 

President Rouhani promised a nuclear deal, to escape the suffocating international sanctions over Iran’s nuclear program that had depressed and distorted the economy and isolated the country. When the treaty was signed in 2015, Iranians rejoiced over the chance, finally, to become a “normal” country.

 

Then came the Trump administration, and its singular focus on Iran as the source, as the defense secretary, Jim Mattis, has said on numerous occasions, of all the troubles in the Middle East.

 

Iran’s leaders have begun promoting patriotic causes, recognizing in the changing mood a chance to reinforce their credibility, which had been flagging.

 

Recently, missiles were put on display in city centers so that families and children could pose for photos in front of them. State-run television has been promoting an annual pilgrimage where millions participate in a walk into neighboring Iraq, as a symbol of national and religious strength. Iran’s supreme leader, Ayatollah Ali Khamenei, who rarely comments on music, cited “Iran, if They Break Your Heart,” a homage to Iran, as one of his favorite songs.

 

Many Iranians now cheer when a missile is tested, said Hamidreza Jalaeipour, a professor of sociology and a leading reformist, “even those who are completely secular.” The tests, he added, “are making them feel strong and safe.”

 

The pressure from the United States and the growing threats from Saudi Arabia are creating an us-against-them atmosphere, Mr. Jalaeipour said.

 

The new solidarity in the face of outside threats is not new in Iran, nor is it insulating the government from all criticism. After the earthquake earlier this month in western Iran, for instance, many people blasted state officials on social media for failing to provide timely help.

 

Yet, in a demonstration of the newfound spirit, Iranians from across the country pitched in to gather water, food and tents and transported the aid with their own cars to the disaster zone.

 

“There is just a big feeling of unity,” said Pouria Gorji, an office manager who personally delivered four trucks of supplies to the quake area. “We come together when we are hurt.”

 

The state’s theocratic ideology has long dictated an artificial version of nationalism, where everything is related to Islam. Love for Iran’s long pre-Islamic history has been illegal in some instances, and often deliberately ignored by officials and state television.

 

But in light of the new national unity, those restraints have been relaxed so long as the presentations encourage patriotism.

 

In October, for example, the government allowed one of the former palaces of the despised, pro-Western shah, Mohammad Reza Pahlavi, to be used for a multimedia performance where actors re-enacted parts of the ‘‘Shahnameh,’’ or book of Kings, which describes Iran’s long pre-Islamic history. The epic poem depicts a time — much like now — when Iran was surrounded by rivals and isolated.

 

One evening around 4,000 middle-class Iranians, with men wearing suits and women carrying designer handbags, gathered at the palace in North Tehran. Images of Persepolis, the ancient palace of Darius the Great, were projected on the building, as famous actors played legendary figures from Iran’s long history.

 

As an encore, a traditional singer, Homayoun Shajarian, performed ‘‘My Iran,’’ a love song to the country. People stood out of respect, many with tears streaming down their faces. The venue was sold out for 30 days straight.

 

“I felt so proud. There was an overwhelming sense of unity,” said Soghol Sheikhan, an accountant, who was at the concert. “I got goose bumps.”

 

Borders and geography are other rallying points for nationalists. When Mr. Trump in October gave a speech outlining his Iran strategy, he called it a “dictatorship” led by “murderous” leaders who “spread death, destruction and chaos all over the globe.” But what really riled up Iranians was when he called the Persian Gulf the “Arabian Gulf.”

 

Within minutes, Iranian news media, state-run and otherwise, began denouncing his use of the name.

 

Two hours later, Mr. Rouhani, in a live rebuttal of Mr. Trump’s speech, wasted no time with the litany of accusations by Mr. Trump and cut straight to the point.

 

“How is it that a president has not learned the name of the international historical gulf?” the Iranian president said. “The same Persian Gulf unfortunately frequented by the American navy, unduly and constantly. Trump should ask his military who have the maps what is written as the name of this gulf.” (In fact, United States Navy maps say ‘‘Persian Gulf.’’)

 

Iranians attacked Mr. Trump’s Instagram account, leaving nearly three million comments about the Persian Gulf, or profanities. An Iranian journalist based in the United States, Bahman Kalbasi, wrote in a tweet that he “couldn’t think of a faster way to unite the public in Iran behind the government: go after the nuclear deal (vital to the economy) and don’t use Persian Gulf.”

There are limits to the new nationalism. Iranians who tried in October to commemorate the ancient King Cyrus with a gathering at his tomb found roads blocked and a high fence around the site. Well-known nationalists were sent text messages warning them not to participate.

 

Arvand Dashtaray, a liberal-minded theater director, who has frequently injected veiled criticism of Iranian society and its leaders into his plays, now says that he and like-minded reformists and moderates were wrong to bet on the United States.

 

Mr. Dashtaray still dreams of bringing the experimental American playwright Robert Wilson to perform in Iran. But as for Mr. Trump, “he is the true face of the U.S., an unfair, weapons-exporting nation that doesn’t care about anybody and especially not about us.”

 

He added: “We need to understand that the U.S. has been playing with us all along. Trump is proving that our hard-liners were right all these years, to say that America cannot be trusted.”

 

Iran’s leaders, on the other hand, long regarded by Mr. Dashtaray as irresponsible in foreign policy, have now “become the logical party,” he said. “Of course, I love them more.”