Two deadly attacks in Catalonia on Thursday and in the early hours of Friday had been planned for some time by people operating out of the town of Alcanar, police said on Friday.
Alcanar was the site of an explosion in a house shortly before midnight on Wednesday, in an incident which police are now linking to the attacks.
Three of the people arrested so far over the attacks are Moroccans and a fourth person in custody is a Spaniard, the regional police chief told a news conference. None had a history of terror-related activities. They were aged between 21 and 34.
Police said they had still not identified the driver of a van who mowed down pedestrians in central Barcelona and then fled the scene on foot.
It was possible that this man was among five suspects shot dead in the resort town of Cambrils early on Friday, but nothing was yet certain, the police chief said.