ISIS claimed responsibility for an attack on a shrine in the Iranian city of Shiraz on Wednesday, according to a statement on the group’s Telegram channel.
Iran’s supreme leader on Thursday vowed to retaliate against those threatening the country’s security.
The gunmen’s attack on the Shiite shrine killed 15 people and wounded dozens, authorities said, and President Ebrahim Raisi promised that the shooting would not go unanswered.
Ayatollah Ali Khamenei said the assailants “will surely be punished” and called on Iranians to unite.
“We all have a duty to deal with the enemy and its traitorous or ignorant agents,” Mr Khamenei said a day after the attack.
Mr Raisi appeared to link the mass protests against the government to the ISIS attack.
“The enemy’s goal is to disrupt the progress of the country. The enemy wants the riots to pave the way for terrorist attacks. The enemy is always an enemy.”
Interior Minister Ahmad Vahidi said the protests sweeping Iran paved the way for the Shiraz attack, and Mr Raisi said Iran would respond, according to state media.
“Experience shows that Iran’s enemies, after failing to create a split in the nation’s united ranks, take revenge through violence and terror,” Mr Raisi said before ISIS claimed responsibility.
“This crime will definitely not go unanswered, and the security and law enforcement forces will teach a lesson to those who designed and carried out the attack.”
CCTV footage broadcast on state TV on Thursday showed the attacker entering the shrine after hiding an assault rifle in a bag and shooting as worshippers tried to flee and hide in corridors.
The semi-official Tasnim news agency said the attacker shot an employee at the shrine entrance before his rifle jammed and he was chased by bystanders.
He managed to unjam his weapon and opened fire on his pursuers, before entering a courtyard and shooting worshippers. Women and children were among the dead, according to the agency.
“I heard sounds of gunfire after we prayed,” a witness told state TV. “We went to a room next to the shrine. This lowlife came and fired a barrage of shots.
“Then [the bullet] hit my arm and leg, it hit my wife’s back, but thank God my child was not hit. He is seven years old.”
Iranian officials said they had arrested a gunman who carried out the attack at the Shah Cheragh shrine.
“We have not been able to interrogate him yet,” deputy provincial governor Easmail Mohebipour told the Tasnim news agency.
State media blamed “takfiri terrorists”, a label Tehran uses for hardline Sunni militants such as ISIS.
The UAE condemned the attack and expressed condolences to the Iranian government and ts people after “this heinous crime”. China said it was shocked by the attack and strongly condemned the incident.
The group has claimed previous attacks in Iran, including deadly twin bombings in 2017 at Iran’s parliament and the tomb of its founder, Ayatollah Ruhollah Khomeini.
Iran has been shaken by more than a month of anti-government demonstrations, the biggest challenge to the regime in a decade.
Thousands of protesters were on the streets of a north-western city to mark 40 days since the death in custody of Mahsa Amini, 22, which sparked the protests.
Shiite Muslims commemorate death 40 days afterwards, typically with an outpouring of grief.
In Amini’s Kurdish home town of Saqez, the birthplace of the nationwide unrest now hitting Iran, crowds walked through the local cemetery and thronged her gravesite.
“Death to the dictator,” protesters shouted at the Aichi cemetery, where women ripped off their headscarves and waved them above their heads.
Other videos showed a large procession making its way along a motorway and through a dusty field towards Amini’s grave.
There were reports of road closures in the area.
While the ayatollah and the president tried to link the ISIS attack to the protests, former Iranian president Mohammed Khatami said that “violence cannot be answered with violence” in a statement carried online.
Mr Khatami’s name and image have been banned in Iranian media since 2015 over his reformist political views, which call for changing the Islamic republic from within.
“If [the people] see that the conditions of this life are not provided [by the government], they have the right to criticise and even protest,” Mr Khatami said.