By Agence France-Presse
The attack in the southwestern city of Ahvaz came as the country marked the anniversary of the start of its 1980-1988 war with Saddam Hussein's Iraq and prompted President Hassan Rouhani to warn of a "crushing response".
"The response of the Islamic Republic of Iran to the smallest threat will be crushing", Rouhani said on his official website. "Those who give intelligence and propaganda support to these terrorists must answer for it."
Foreign Minister Mohammad Javad Zarif said in a tweet that the attack near the Iraqi border was carried out by "terrorists recruited, trained, armed & paid by a foreign regime".
"Iran holds regional terror sponsors and their US masters accountable for such attacks," he wrote.
IS jihadists said via their propaganda mouthpiece Amaq that "Islamic State fighters attacked a gathering of Iranian forces" in Ahvaz.
The city lies in Khuzestan, a province bordering Iraq that has a large ethnic Arab community and has seen separatist violence in the past that Iran has blamed on its regional rivals.
State television gave a casualty toll of 29 dead and 57 wounded, while official news agency IRNA said those killed included women and children among spectators at the rally. Many of the wounded were in critical condition.
Armed forces spokesman Brigadier General Abolfazl Shekarchi said the dead included a young girl and a former serviceman in a wheelchair.
"Of the four terrorists, three were sent to hell at the scene, while the fourth who had been wounded and arrested went to hell moments ago due to his severe wounds," Shekarchi told state television.
Khuzestan deputy governor Ali-Hossein Hosseinzadeh told the semi-official ISNA news agency that "eight to nine" troops were among those killed, as well as a journalist.
Finger pointed at Riyadh
Zarif did not specify which regional government he held responsible for the shooting, but Iran's elite Revolutionary Guards said the attackers were funded by Sunni arch-rival Saudi Arabia.
"Those who opened fire on civilians and the armed forces have links to the Ahvazi movement," Guards spokesman Ramezan Sharif told ISNA.
"They are funded by Saudi Arabia and attempted to cast a shadow over the Iranian armed forces."
Zarif vowed Iran would "respond swiftly and decisively in defence of Iranian lives".
In a message of condolence to Russia's close regional ally, President Vladimir Putin said he was "appalled by this bloody crime".
"This event once again reminds us about the necessity of an uncompromising battle against terrorism in all of its manifestations."
Khuzestan was a major battleground of the 1980s war with Iraq and the attack on the anniversary parade in Ahvaz had significant symbolic value.
The province saw unrest in 2005 and 2011 but has since seen been largely quiet.
Attacks by Kurdish rebels on military patrols along the border further north are relatively common.
But attacks on regime targets inside major cities are far rarer.
On June 7, 2017, 17 people were killed and dozens wounded in simultaneous attacks in Tehran on the parliament building and on the tomb of revolutionary leader Ruhollah Khomeini -- the first inside Iran claimed by the Sunni Muslim extremists of the Islamic State group.
In April, 26 alleged IS jihadists went on trial on charges connected with that twin attack.
The attack in Ahvaz came as President Rouhani was among dignitaries at the main anniversary parade in Tehran.
In a keynote speech, Rouhani vowed to boost Iran's ballistic missile capabilities despite Western concerns that were cited by his US counterpart Donald Trump in May when he abandoned a landmark nuclear deal with Tehran.
"We will never decrease our defensive capabilities... we will increase them day by day," Rouhani said at a military parade. "The fact that the missiles anger you shows they are our most effective weapons," he said, referring to the West.
Iran has ballistic missiles with a range of up to 3,500 kilometres (2,200 miles), enough to reach both Israel and US bases in the Middle East.
The United States reimposed crippling sanctions on Iran last month, and a new round of even harsher sanctions targeting Iran's vital oil sector is set to go back into effect on November 5.
Washington has said it is ready to open talks on a new agreement to replace the July 2015 accord, but Tehran has said repeatedly it cannot negotiate under the pressure of the sanctions.
Trump and Rouhani will both be in New York next week for the United Nations General Assembly. But Iran has repeatedly ruled out any meeting.