UTRECHT, Netherlands — Gunshots shattered the tram ride in a heavily Muslim neighborhood of this old Dutch city. The prime minister called it possible terrorism. SWAT teams rushed in, residents were ordered indoors, and all mosques closed as the police sought a killer in a country that had been spared large-scale terrorist attacks.

Suddenly the mayhem on Monday in the Netherlands seemed as if it might be another planned public slaughter in a seemingly tranquil part of the world, like the mosque massacre that had traumatized the people of Christchurch, New Zealand.

The suspect, it turned out, was a Turkish immigrant described by acquaintances as a sometimes-religious man with a criminal record who may have been entangled in a dispute with his ex-wife.

It was about 10:30 a.m. when a gunman opened fire on the tram in Utrecht’s Kanaleneiland neighborhood, heavily populated by Turkish and Moroccan immigrants. Someone slammed on the emergency brakes, and other passengers screamed, clamoring to escape, according to witness accounts. At least three people were killed and five injured, some by shattering glass, and the assailant stepped off the tram and fled.