"The terrorist actions in Syria and Iraq are a serious threat to the world. If there is not a serious battle against terrorism in the region, we will see several terrorist governments and entities emerge in the North African region," said Rouhani, according to the presidency's website.
The Islamic State jihadist group has spread from its bases in Syria and Iraq to gain a significant foothold in Libya, and it has also carried out attacks in Algeria and Egypt.
Mogherini also met with Foreign Minister Mohammad Javad Zarif during her visit to Tehran for "high-level talks" on the Syria crisis.
Signup to our newsletter and follow us on Facebook and Twitter!
The EU official, who was due to fly on to Iran's regional rival Saudi Arabia, was quoted by local media as saying the EU "needed the cooperation of Iran, a key power for solving the region's problems".
Rouhani called on the European Union to put pressure on regional powers to cut support to rebel groups in Syria.
Iran provides financial and military support to Syrian President Bashar al-Assad, and it accuses Saudi Arabia and Turkey of funding the rebel groups it is fighting.
"The fight against terrorism in Syria and Iraq is the priority," said Rouhani, adding that the territorial integrity of both countries must be preserved.