Rouhani did not name any country, but officials and media in mainly Shiite Iran have hinted that insurgents from the Islamic State in Iraq and the Levant (ISIL) are being financially and militarily supported by Saudi Arabia and Qatar.
"I advise Muslim countries that support the terrorists with their petrodollars to stop," Rouhani said in remarks reported by the website of Iran's state broadcaster.
"Tomorrow you will be targeted... By these savage terrorists. Wash your hands of killing and the killing of Muslims," he added.
Riyadh has warned that Iran-ally Prime Minister Nuri al-Maliki is steering Iraq towards civil war through policies that exclude the country's Sunni minority.
More From This Section
Iran, the predominant Shiite powerhouse in the Middle East, says it will support Maliki against ISIL, which is also battling the regime of Syrian President Bashar al-Assad, another Tehran ally.
Rouhani called for unity between "Shiites and Sunnis who are brothers".
"For centuries, Shiites and Sunnis have lived alongside each other in Iran, Iraq, the Levant, Lebanon, the Persian Gulf and North Africa...In peaceful coexistence," he said.
"The Shiites, Sunnis and Kurds are our friends," parliament speaker Ali Larijani said in remarks reported on the Majlis website.
"We have always insisted that all ethnic groups must have active and constructive participation in Iraq's power structure.