Thirteen people died and dozens were injured when a van ploughed into crowds in Barcelona's famous Las Ramblas area, Catalan police and local officials say.
The rented vehicle sped along the pedestrian area that is popular with tourists, mowing down people and sending others fleeing for cover.
Spain's Prime Minister Mariano Rajoy said it was a "jihadist attack".
Two people have been arrested, but police say neither was the driver, who fled on foot.
Early on Friday morning, Catalan police said an operation was taking place in town of Cambrils, to the south-west of Barcelona, in response to a "possible terrorist attack" and have told people there to stay indoors.
The authorities also say an explosion on Wednesday evening at a house outside Barcelona that left one person dead was connected to the Las Ramblas attack.
Dozens of gas cylinders were found at the house in the town of Alcanar, about 100km (60 miles) south west of Barcelona, and Reuters quoted a police source as saying residents were preparing explosives.
In another incident, police shot dead a man who drove into officers at a checkpoint on the outskirts of Barcelona, but the authorities said there was no evidence that he was connected to the attack in Las Ramblas.
Police have released a photo of a man named as Driss Oukabir, whose documents were used to rent the van involved in the attack.
Local media say he is in his 20s, and was born in Morocco. However, latest reports suggest he has told police he was not involved, and that his documents were stolen.
So-called Islamic State has said it was behind the van attack in Las Ramblas, saying in a brief statement carried by its Amaq news agency that it was carried out by "Islamic State soldiers". The group gave no further evidence or details to back this claim.
The attack happened at the height of the season in one of Europe's top tourist destinations.
Witnesses said the van deliberately targeted people, weaving from side to side as it drove down the boulevard.
Vehicles have been used to ram into crowds in a series of attacks across Europe since July last year.
Analysis: A worrying trend
Gordon Corera, BBC News security correspondent
Barcelona is just the latest European city to witness the terrible effects of a vehicle attack on an iconic or "soft" target.
In Nice a year ago, Bastille Day celebrations were targeted, then a Christmas market in Berlin. In London, Westminster and London Bridge, as well as Finsbury Park, saw cars and vans used as weapons.
In the UK, new barriers have been put in place to mitigate some of the risks at key locations and there is discussion of imposing checks on those renting vans, but security services in Britain - as well as across Europe - are all too aware that there are limits to what they can do to spot and stop those planning murder in this way.
The weapons employed are readily available and there is little or no training, co-ordination or planning required. This means there are few points where individuals can be spotted by the intelligence services.
Although so-called Islamic State has released a statement saying what it calls its "soldiers" had carried out the Barcelona attack, it is not yet clear whether there was any direct link to the group or if they were simply inspired by its call to act. The language used often indicates the latter.
Either way, authorities will be bracing themselves for the possibility of further attacks of a similar nature.
What did people see?
A businessman from New Orleans, who was just arriving in a taxi in Las Ramblas, said: "I heard a crowd screaming. It sounded like they were screaming for a movie star.
"I saw the van. It had already been busted on the front. It was weaving left and right, trying to hit people as fast as possible. There were people lying on the ground."
Aamer Anwar said he was walking down Las Ramblas, which was "jam-packed" with tourists.
"All of a sudden, I just sort of heard a crashing noise and the whole street just started to run, screaming. I saw a woman right next to me screaming for her kids," he told Sky News.
"Police were very, very quickly there, police officers with guns, batons, everywhere. Then the whole street started getting pushed back.
"Police officers who got there just started screaming at people to move back, move back."
Kevin Kwast, who is on holiday in Barcelona with his family, said: "I was eating with my family in La Boqueria market very near where the crash occurred.
"Hundreds of people started stampeding through the market... we started running with them going outside right into where casualties were already on the ground.
"Police pushed us into a money transfer shop and we've been sheltering there for over an hour."
How have leaders reacted?
Prime Minister Rajoy has travelled to Barcelona and declared three days of national mourning.
"Terrorists are defeated by unity," he said.
Barcelona's mayor, Ada Colau, said the city would hold a minute of silence on Friday at midday.
The United States has offered its assistance to Spain.
Secretary of State Rex Tillerson also said consular assistance was being provided to Americans in the city, and urged US nationals to check in with their families.
"Terrorists around the world should know the United States and our allies are resolved to find you and bring you to justice," he told reporters.
UK Prime Minister Theresa May said her thoughts were with the victims, adding that "the UK stands with Spain against terror".
French President Emmanuel Macron said: "We remain united and determined."
- Central boulevard that runs 1.2km (0.75 miles) through the centre of Barcelona
- Runs from the city's Plaça de Catalunya (Catalonia Square) to the Christopher Columbus monument at the seafront.
- Popular with tourists because of its market stalls, bars and restaurants
- Barcelona city council restricted traffic flow because of heavy pedestrian use of the street
Europe's deadly vehicle attacks
- Paris, 9 August 2017: A man rammed a BMW into a group of soldiers, injuring six.
- London, 19 June 2017: A man is killed in a van attack on Muslims outside a mosque in Finsbury Park.
- London, 3 June 2017: Eight people died when three jihadists drove a van into pedestrians on London Bridge and then stabbed passers-by.
- Stockholm, Sweden, 7 April 2017: Uzbek Rakhmat Akilov killed four people when he drove a lorry into a department store.
- London, 22 March 2017: Four people died when a car rammed into pedestrians on Westminster Bridge, and the driver then stabbed to death a policeman.
- Berlin, Germany, 19 December 2016: Tunisian Anis Amri ploughed a truck into a Christmas market at Breitscheidplatz, killing 12 people.
- Nice, France, 14 July 2016: Tunisian Mohamed Lahouaiej-Bouhlel drove a truck into crowds on the Promenade des Anglais, killing 86 people on Bastille Day.
- France, December 2014: A van was driven into a Christmas market in Nantes and a car rammed pedestrians in Dijon, leaving more than 20 wounded.