Officers detained two pairs of brothers, Spaniards of Moroccan origin, in the Spanish territory of Ceuta bordering Morocco, the interior ministry said in a statement.
It said they had "a very similar profile" to the killers in this month's Islamist attacks in Paris, but made no material link between the two cases.
The four were inspired by online messages from the armed group calling itself Islamic State (IS), which controls parts of Iraq and Syria.
"They formed part of a jihadist cell fully prepared and willing to launch an attack in Spain," the statement said.
It did not say whether the four had made any concrete plans for an attack.
"They have acquired a high level of radicalisation and are even prepared to die in committing a terrorist act," the statement said.
Police seized a nine-millimetre automatic pistol and machetes among other items during dawn raids on two properties. They were the latest in a string of arrests in Ceuta and Spain's other north African territory, Melilla, where authorities have been monitoring suspected extremist cells.
Separately, Spanish authorities are investigating suspects linked to the attacks in Paris and other foiled plots in Belgium who are said to have travelled to Spain. Spanish police have arrested about 50 suspected jihadists over the past year, the ministry said this month. Many of them are suspected of planning to join IS.
Fearing a rise of "homegrown" and "lone wolf" extremists in Spain, the government has been cracking down on their recruitment online.
On March 11, 2004, Al-Qaeda-inspired bombings killed 191 people in an attack on Madrid commuter trains.