Spain was hit by its worst terrorist attack in more than a decade on Thursday, when a van driver plowed into dozens of people enjoying a sunny afternoon on one of Barcelona’s most famous thoroughfares, killing at least 13 people and leaving 80 bloodied on the pavement.

Hours later, the Catalan police said they foiled a second vehicular attack, in the seaside town of Cambrils, 70 miles to the south, fatally shooting four people. A fifth died later of wounds, the police said. The suspects appeared to be wearing explosive belts, though these devices were later found to be fake, police said. Six civilians and one police officer were injured during the episode, the Catalan emergency services said.

The Barcelona attack was at least the sixth time in the past few years that assailants using vehicles as deadly weapons have struck a European city.

The police cordoned off the Plaza de Cataluña and Las Ramblas in the heart of Barcelona, both tourist destinations, and began a chaotic pursuit for the attackers.

Three people were arrested, including a Moroccan man whose identification documents had been used to rent the van. The Barcelona police said none were believed to be the driver, who escaped on foot and remained at large.

The Islamic State claimed responsibility for the Barcelona assault, which shattered a peaceful afternoon in one of Europe’s most picturesque cities. President Trump and other Western leaders quickly condemned the attack and pledged cooperation.

In a sign of the confusion that prevailed after the attack, local television reported that one assailant, armed with a rifle, had run into a restaurant and was besieged by the police. The police said the entire version was false.

Witnesses described people screaming and running for their lives as the van driver wove back and forth just after 5:30 p.m., apparently trying to hit as many people as he could. Police officers swept through the area near Las Ramblas, a wide boulevard with a large pedestrian section, moving people out of the area.

Videos taken by witnesses and posted online showed men, women and children motionless on the ground amid broken umbrellas and chairs, in the shade of trees, many bleeding profusely. Paramedics and friends knelt to comfort them as police sirens wailed.

Whitney Cohn, a mathematics teacher from Montebello, N.Y., was walking along the mall with her husband and two daughters, on the way back to their hotel after visiting a museum, when the van came careening through the crowd, throwing people aside like dolls as screams pierced the air. She grabbed her daughters and started running. “It was flying,” Ms. Cohn said in a text exchange from a nearby restaurant. “The van missed us by a sec.”

Other witnesses described chaos as people dropped their belongings and fled as the van entered the mall and accelerated, hitting people indiscriminately, among them children and the elderly. Among the seriously injured was a 6-year-old girl hospitalized with a cerebral hemorrhage, an official at Vall Hebron University Hospital said.

People streamed onto side streets, many of them weeping. “It was horrific,” said Sergi Alcazar, a 25-year-old photographer who arrived 10 minutes after the attack to find victims lying amid broken umbrellas, chairs and cafe tables.

Until Thursday, Spain had been spared from the recent wave of terrorist attacks in Europe — many involving vehicles plowing into crowds — claimed by extremists in France, Germany, Britain and elsewhere.

Keith Fleming, an American who lives just off Las Ramblas, told The Associated Press that he was watching television when he heard a noise, looked out over his balcony and “saw women and children just running and they looked terrified.”

Mr. Fleming said the street was deserted, except for police officers with guns drawn or in riot gear. “It’s just kind of a tense situation,” The A.P. reported him as saying. “Clearly people were scared.”

Maj. Josep Lluis Trapero, a senior police official in Spain’s Catalonia region, said at a news conference that the police were investigating a possible connection between the van attack and a gas explosion the previous night in Alcanar, a town south of the city, which killed one person and injured several others.

One person, a Spaniard from the Spanish territory of Melilla in Morocco, had been taken into custody in Alcanar, he said. A second man, identified as Driss Oukabar, a Moroccan citizen, was arrested in the northern Catalan town of Ripoll when he walked into a police station and reported that his documents had been stolen.

Major Trapero said neither man appeared to be the driver of the van, which came to a stop near Barcelona’s opera house. The driver escaped on foot, he said.

“It was clearly a terror attack, intended to kill as many people as possible,” Major Trapero said.

A third person was later detained in Ripoll, about 65 miles north of Barcelona.

A national police official, speaking on the condition of anonymity to discuss a continuing investigation, said at least three vans had been rented under Mr. Oukabar’s name. As night fell, the Barcelona police were frantically searching for the two other vans, combing the streets and underground parking garages, the official said.

A counterterrorism expert, who was briefed on the details of the investigation, said late Thursday night that the police now believed the plot initially involved the use of explosives and a large truck.

“Part of the plan was they tried to rent a larger truck, but they didn’t have the right permit and so they ended up getting” smaller vans, said the expert, who requested anonymity to share information that had been disclosed to him in confidence.

He added that the building in Alcanar where the explosion occurred had been packed with gas canisters, as well as other materials used to make explosives.

American counterterrorism officials in Washington said they were in contact with the Spanish authorities to offer any assistance, but underscored that the investigation had just started.

Mr. Trump said on Twitter that the United States would “do whatever is necessary to help,” telling Spaniards to “be tough & strong, we love you!”

Pro-Islamic State accounts on the Telegram messaging service shared news of the attack. One channel, called “Expansion of the Caliphate,” posted video of the scene alongside a message in Arabic. “Terror is filling the hearts of the Crusader in the Land of Andalusia,” it said.

In the past year, the Islamic State, also known as ISIS or ISIL, has devoted resources to translating its channels and messages into Spanish.

Although countries like France and Britain have repeatedly been named in ISIS propaganda urging followers to stage attacks, Spain has been less in the cross hairs.

The country has, however, been a transit point for recruits of the militant group, both for those going to Syria and those returning. The Spanish police arrested nine people in April who they said may have been connected with deadly attacks in France and Spain.

The Barcelona attack appeared to follow the playbook of recent assaults in which attackers drove vehicles into crowded stretches of large European cities.

“While it’s not clear whether the attackers corresponded with ISIS prior to the operation, it’s clear that the methods used in the attack is something ISIS encouraged and incited over and again,” said Laith Alkhouri, a director in New York of the business-risk intelligence company Flashpoint.

In the French city of Nice, a man drove a rental truck into a crowd celebrating Bastille Day on the seaside Promenade des Anglais last year, killing 86 people.

A few days before Christmas last year, a driver in a stolen truck mowed downshoppers at a holiday market in Berlin, killing 12 people and wounding dozens.

At least seven civilians were killed and dozens injured in June when knife-wielding assailants sped across London Bridge in a white van, ramming numerous pedestrians before emerging with large hunting knives to attack the capital’s Borough Market, a crowded nightspot.

ISIS claimed responsibility, saying the attack had been carried out by “a detachment of Islamic State fighters.”

That assault was reminiscent of another, on Westminster Bridge in London in March, when Khalid Masood, 52, drove a car into pedestrians, killing four. He then fatally stabbed a police officer near Parliament before he was shot and killed. The police treated that attack, in which 50 were injured, as “Islamist-related terrorism.”

There have been other deadly attacks using vehicles that were not related to Islamist extremists. A British man rammed a rental van into Muslims leaving prayers in North London during Ramadan, and a man who was part of white supremacist demonstrations in Charlottesville, Va., is accused of driving his car into a crowd Saturday, killing a woman.

In 2004, a series of bombs ripped through commuter trains in Madrid, killing 191 and wounding over 1,800. The bombings were carried out by a group of North African Islamists that intersected with a band of petty criminals.

Leaders of European countries and cities that have suffered attacks quickly expressed solidarity with Barcelona.

In Germany, which has been on alert for potential terrorist threats ahead of the general election on Sept. 24, members of Chancellor Angela Merkel’s cabinet showed their support.

“I am deeply shaken by the terrible news from Barcelona,” said Thomas de Maizière, Germany’s interior minister. “Once again, terror has shown its grotesque face.”