LONDON—Wednesday night was expected to be another night of violence across the U.K. Far-right protesters had promised to target asylum centers, law offices and hotels used to house migrants. Storefronts were boarded up and thousands of police deployed across the country.

Instead, tens of thousands of counterprotesters turned out in cities from London to Sunderland in England’s North East, outnumbering the hundreds of far-right protesters who had been responsible for the worst civil unrest the country has seen in over a decade. There were few reports of violence. Some in the crowds held up placards saying “Refugees are welcome here” and “Love + Unity.”

The largely peaceful night gave hope that the worst of the unrest was over after a week of violent riots that began following the killing of three children by a knife-wielding attacker who was falsely reported on social media to be a migrant who entered the U.K. illegally. “I am really pleased at how it went,” said London’s Metropolitan Police Commissioner Mark Rowley on Thursday.

While more far-right protests could still flare in the coming days, government officials were cautiously optimistic that a strategy of quickly prosecuting violent offenders and publicly naming them was paying off. In recent days, courts fast-tracked cases against dozens of people who had been arrested battling police or destroying shops.

People gather against an an anti-immigration protest, in London, Britain, August 7, 2024. REUTERS/Chris J Ratcliffe

One Liverpool man received a three-year jail sentence for attacking a police officer, and two others received 20- and 30-month terms. A 69-year-old retiree who wielded a wooden bat when officers tried to arrest him was jailed for two years and eight months. One defendant was a 14-year-old boy.

The front page of Britain’s Sun tabloid ran mug shots of 15 protesters who had been jailed with the headline “Nailed and Jailed.”

“This is the swift action we’re taking. If you provoke violent disorder on our streets or online, you will face the full force of the law,” said Prime Minister Keir Starmer , who was chief prosecutor and put hundreds of rioters in jail in 2011, the last time the nation saw disorder on this scale.

People gather against an an anti-immigration protest, in London, Britain, August 7, 2024. REUTERS/Chris J Ratcliffe

“What we saw last night were the vast majority of the law-abiding people of this country expressing a view that we live in a tolerant Britain,” said Policing Minister Diana Johnson.

Johnson said it was still too early to say whether the protests would fizzle out. There could be further disorder over the weekend as far-right Facebook groups planned more gatherings across the country.

More than 100 counterprotests are also planned for the weekend, said Weyman Bennett, joint national secretary of Stand Up to Racism, a group that helps organize campaigns against the far-right. Bennett said that he still expected further clashes as far-right groups remain emboldened.

The unrest began shortly after an assailant last week attacked a children’s dance class in Southport, near Liverpool, killing three young girls. The 17-year-old attacker was wrongly identified on social media as a man named Ali Al-Shakati who had moved to the country illegally and been on a government terror watch list. Authorities quickly denied that and took the unusual step of identifying the suspect as the Welsh-born son of parents from largely Christian Rwanda, who had settled in the U.K. decades ago.

Despite that, violent riots broke out in parts of the country, with small mobs of mostly young men attacking police officers and damaging buildings including local libraries and hotels that they suspected of containing asylum seekers.

In Sunderland, a taxi carrying two Filipino nurses to work was pelted with rocks. A Holiday Inn in central England was attacked by a mob who believed it to be housing asylum seekers. The Royal College of General Practitioners, which represents doctors, said there had been “horrific and unacceptable” verbal abuse and violence against medical workers, especially ethnic minorities.

Far-right groups had promised an even bigger turnout Wednesday night, sparking concern, especially among immigrant communities. Instead, counterprotesters, including many from the Muslim community, turned out in large numbers.

People hold a banner on the day of a protest against illegal immigration outside of Downing Street in London Britain, July 31, 2024. REUTERS/Hollie Adams

The government also vowed to pursue those encouraging the violence online. The wife of a Conservative Party councilor was arrested for calling for hotels housing asylum seekers to be burned. A 53-year-old woman was arrested after sending a message threatening to blow up a mosque.

Both legal and illegal migration have risen to record levels in recent years, at a time of weak economic growth and stagnant wages.

Legal net migration, driven in part by a labor shortage in the economy as well as a government policy to allow in refugees from Ukraine and Hong Kong, hit a record 764,000 in 2022 before falling to 685,000 last year. It is expected to fall further this year.

The U.K. is struggling, like the U.S., with a surge in asylum seekers—in this case, people who cross the English Channel on small boats to claim refugee status. That hit a high of 45,774 in 2022 and is on pace for another record this year.

John Hayes, a 63-year-old who was stabbed while trying to stop the Southport attacker, condemned the riots but urged the government to understand why some are angry after years of government promises to reduce immigration.

There has been a “strong undertone of discontent for some time about levels of immigration and [the attack] was just a catalyst,” he said.

Bennett, of Stand Up to Racism, said that while there is a long tradition in the U.K. of protesting against the far-right and fascist groups “there is real disquiet in the country about poverty and structural decline but people have been given a false narrative” about migration.

Write to Max Colchester at Max.Colchester@wsj.com and David Luhnow at david.luhnow@wsj.com