Twin attacks on the Iranian parliament and Ayatollah Khomeini's mausoleum in the capital, Tehran, have killed at least 12 people and injured many more.
After hours of intermittent gunfire, officials said both incidents were over and the attackers were dead.
The Islamic State (IS) group has said it carried out the attacks, which would be a first in Iran.
Iran's powerful Revolutionary Guards accused Saudi Arabia and the US of being behind the attacks.
"This terrorist action, coming one week after the meeting of the president of the United States with the leader of the one of the region's reactionary governments (Saudi Arabia)... shows they are involved in this savage action," it said in a statement.
The US state department condemned the attack, saying "the depravity of terrorism has no place in a peaceful civilised world".
President Hassan Rouhani said Iran's message was that "terrorism is a global problem", and he called for "regional and international co-operation and unity".
Iran's Supreme Leader Ayatollah Ali Khamenei said the attacks would have "no effect" on the country.
In a statement, IS warned of further attacks against Iran's Shia Muslim majority.
How did the attacks unfold?
Gunmen armed with Kalashnikovs entered the parliament on Wednesday morning. Images from the scene showed a major security operation as forces surrounded the building.
Heavy gunfire could be heard.
Iranian authorities denied that there had been a hostage situation inside the parliament building.
Reports said the gunmen had entered parliament via a public entrance, dressed as women.
Security forces surrounded the parliament, and TV footage showed people, including children, escaping from windows.
Iranian media later reported that four attackers inside the parliament building had been killed by security forces.
At the mausoleum in southern Tehran, which is dedicated to the Islamic Republic's founder Ayatollah Khomeini, attackers began their assault by opening fire.
The governor of Tehran said one attacker there had detonated a suicide vest and another had been killed by security forces, state broadcaster Irib reports.
Images from the scene showed grenades and magazines for automatic weapons, apparently recovered from the body of an attacker.
The suicide attacker was a woman, reports suggested.
It is not clear whether the death toll of 12 includes the attackers, or whether the victims were killed at both incidents or solely at the parliament.
About 40 people were injured in the two attacks, according to emergency services chief Pir Hossein Kolivand.
Iranian officials say they managed to foil a third attack.
What kind of presence does IS have in Iran?
IS said it had carried out the attack and later posted a video which showed what it claimed was footage from inside the parliament building.
A voice is heard saying, in Arabic: "We're not going anywhere. We're staying forever."
An Iranian MP later confirmed that the video showed his office and one of his employees lying dead.
BBC Persian's Jenny Norton says that despite Iran's active involvement in fighting IS in both Iraq and Syria, the Sunni group has not until now carried out any attacks inside Iran, and appears to have little support in this predominantly Shia country.
However, our analyst says, in recent months the group has stepped up its Farsi-language propaganda efforts - targeting Iran's restive Sunni minority.
An IS documentary-style video in March featured militants who were introduced as Iranian fighters in IS ranks based in Iraq.
Speaking in Farsi, they denounced the Iranian government and the religious establishment, including the country's spiritual leader Ayatollah Ali Khamenei.
Iranian intelligence agencies claim to have foiled a number of IS-inspired plots.
But by mounting a successful attack, IS could claim a major coup against a traditional foe that other Sunni jihadist groups, including its rival al-Qaeda, have failed to target in the past.
What has been the reaction both inside Iran and abroad?
This is the most serious terrorist violence in Tehran since the turbulent early years after the 1979 Islamic Revolution.
It will come as a huge shock to ordinary Iranians, who have got used to living in a country which is generally far more stable and safe than most of its neighbours, our analyst says.
Parliament Speaker Ali Larijani downplayed the events, describing them as a "minor issue".
However, officials announced a nationwide state of emergency in response to the attacks.
Later, the Revolutionary Guards said the fact that IS had claimed the attacks "proved" that Saudi Arabia was behind them.
"This terrorist attack happened only a week after the meeting between the US President [Donald Trump] and the [Saudi] backward leaders who support terrorists," a statement said.
Iran's ally Russia condemned the attacks.
"The continuation of a series of terror attacks again underlines the need for co-ordinated actions in the fight against terror and IS," Kremlin spokesman Dmitry Peskov said.
France also condemned the attacks, but some Western countries have appeared slow to react.
What is the likely effect of the attacks?
Middle East analyst Dina Esfandiary says one possible consequence will be increased calls by hardliners for tougher action against IS in Iraq and Syria.
Public support for action in Iraq is likely to grow, as it did when IS took swathes of territory in the country in 2014.
But Iran's involvement in Syria is not popular, our analyst says - it is seen as having few benefits and costing too many Iranian lives.
The attacks will also boost the popularity of the Revolutionary Guards, seen as protectors of the nation.