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.
ISIL militants have seized a swathe of Iraqi territory in a lightning offensive, with the Baghdad government's security forces hard-pressed to prevent the advance.
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.
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.
Since ISIL began its Iraq offensive, Tehran has urged Iraqis to unite against the jihadists.
"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."