The Guardia Civil force said it had arrested the man, described as deeply radicalized, after a joint investigation with security forces in Morocco and the United States.
"The suspect intended to carry out a terrorist act, the details of which are unknown," police said in a statement on Friday.
His links to Islamic State date back at least four years, police said, though he had not displayed any outward signs of radicalization until recently.
After Spain imposed a coronavirus lockdown on March 14, his interest in the jihadist movement increased, police said.
Since then, the suspect made public pledges of allegiance to Islamic State and declared his hatred of the West on social media, police said.
He also made several furtive trips around the city, during which police believe he was scouting out potential targets for an attack.
Police said he was likely to have been responding to calls from Islamic State for followers in the West to carry out attacks in the countries where they live.
In April, security forces in the southern city of Almeria captured arrested a British rapper who was on the run after fighting for the jihadist movement in Iraq and Syria.