"Iran is critically important in the region," Foreign Minister Jose Manuel Garcia-Margallo said on the sidelines of a business forum in Madrid.
"I hope that if a solution is found for the Iranian nuclear issue, Iran could join the fight" against a phenomenon "which threatens all Muslims, be they Shia or Sunni," he added.
"Iran exercises enormous influence in the Shia world and has ties with movements that operate in the region."
His comments came two days after Iran and six world powers wrapped up their latest round of talks in Geneva to try to reach an agreement on Tehran's nuclear programme.
The European Union, which is the lead negotiator for the powers, said the talks had been "serious and useful" and the next round of discussions would be held in early February.
Signup to our newsletter and follow us on Facebook and Twitter!
Britain, China, France, Germany, Russia and the United States are seeking a lasting agreement to allay international concerns about Iran's nuclear ambitions in exchange for relief from crippling Western sanctions.
Iran has blamed the West for the rise of the group calling itself Islamic State, which controls large swathes of Syria and Iraq, but also suggested the need for common action in confronting extremists.
"Against Islamic State or against its competitor in horror, Al-Qaeda and its diverse branches, there are no other solutions but the military solution," Garcia-Margallo said.
Spain, which has one of the rotating seats on the UN Security Council, will send 300 soldiers to Iraq at the end of January to help train the country's soldiers to fight Islamic State.
The soldiers will be part of the US-led coalition fighting the group and they will train Iraqi troops in explosives handling, land mine clearance and special operations.
The Spanish foreign minister is scheduled to attend meeting in London on Thursday of nations taking part in the coalition against the group, hosted by US Secretary of State John Kerry and British Foreign Secretary Philip Hammond.