“Today’s rally shows that the government does not want any confrontation with the U.S.,” said Farshad Ghorbanpour, an analyst who is close to the government of President Hassan Rouhani. “Don’t be surprised, we have no interest with tensions.”

Throughout the week, Iranians on social media had asked people not to burn flags, but instead to thank American protesters for standing up to Mr. Trump’s targeted travel ban and for defending refugees, students, tourists and others affected by the executive order. Others said they had underestimated the new president at first, but now worried about new sanctions on Iran, or even military strikes.

Most of the hundreds of thousands at the rallies carried signs handed to them at the starting points of the rally. Several people carried black and white signs with text in English calling on Americans to visit Iran.