Traffic was diverted from roads leading up to Azadi (freedom) square ahead of a march to remember the day in 1979 when Ayatollah Ruhollah Khomeini ousted the shah's last government.
State television showed crowds defying cold rainy weather and carrying Iranian flags while shouting "Death to Israel, Death to America" - trademark chants of the revolution.
In a speech at Tehran's Azadi (Freedom) square, Rouhani said USA efforts to isolate Iran would fail.
"We do not and we will not ask permission for producing any type of missiles from anybody", Rouhani said in his speech Monday, though he stressed that Iran would "continue constructive engagement" with the global community.
"I also want to share with you that the world has been in awe of Iran's military power and its capabilities over the last 40 years and especially in the last five years..."
Stalls set up by state institutions handed out tea and cakes and choirs sang revolutionary songs, as crowds massed despite driving rain.
The crowd of demonstrators in the rallies paid tribute to the late founder of the Islamic Republic, Imam Khomeini, under whose leadership the revolutionary movements triumphed over the despotic regime of Pahlavi on February 11, 1979.
State television offered blanket coverage of the commemorations, showing marchers in cities ranging from Abadan in south-western Iran to Mashad in the northeast, all joined in fervent denunciations of both the US and Israel.
"I bow in admiration to Iran's resilient people who - despite hardships and grievances - today poured into streets by the millions to mark 40th anniv of their Islamic Revolution, which some in the U.S. wished would never come", Iranian Foreign Minister Mohammad Javad Zarif tweeted.
"It has been moving forward over the past 40 years and making Iran stronger".
In January, Rouhani said Iran was dealing with its worst economic crisis since the Shah was toppled.
The Iranian people "will not stop saying "Death to America" as long as the USA acts malicious" toward Iran, Khamenei said, referring to Trump's State of the Union address in which the American president, among other things, said: "We will not avert our eyes from a regime that chants death to America".
"We are here to support the revolution", the 57-year-old pensioner, who refused to give his first name, told AFP at the event in Tehran.
Israeli Prime Minister Benjamin Netanyahu has responded to the IRGC's harsh rhetoric about attacking Israel on Monday, saying that although he was "not ignoring the threat of the Iranian regime", he was also "unimpressed by them".
The large turnout in state-sponsored rallies came as Iranians face mounting economic hardships.
Before the fireworks, supporters of the revolution shouted chants of "Allahu Akbar" (God is greatest) from rooftops, recalling the protests that swept Khomeini to power four decades earlier.