By CHARLOTTE GRAHAM-McLAY, AUSTIN RAMZY and DANIEL VICTOR, New York Times–

A gunman opened fire on two mosques in Christchurch, New Zealand, on Friday, killing 49 people in an afternoon slaughter that in part was broadcast live online after the publication of a white supremacist manifesto.

Jacinda Ardern, New Zealand’s prime minister, said that in addition to those killed, 20 were seriously injured, shaking a country with little history of mass shooting in what she called “an extraordinary and unprecedented act of violence.”

Some of the shootings were streamed on Facebook, a grim development in terrorism that raised questions about the ability of tech companies to block violent content.

The police said three men and one woman were in custody. One of those in custody is Australian, said the country’s prime minister, Scott Morrison.

New Zealand’s police commissioner, Mike Bush, said that a number of explosive devices were found in vehicles stopped by the police. The police warned residents of central Christchurch to stay indoors and asked mosques to close.

“This is and will be one of New Zealand’s darkest days,” Ms. Ardern said at a news conference.

“It is clear that this can only be described as a terrorist attack,” she added.

Mr. Bush said a mosque on Deans Avenue near Hagley Park, in the center of the city, and a mosque on Linwood Avenue, about three miles away, were attacked.

The attacks began at about 1:40 p.m., not long after the midday Friday prayer, when the mosques would be most busy. Ms. Ardern said that many of the victims were most likely migrants to New Zealand or even refugees.

“They are one of us,” she said. “The person who has perpetrated these acts is not.”

Shortly before the shooting, someone appearing to be the gunman publicly posted links to a manifesto on Twitter and 8chan, an online forum. The 8chan post included a link to what appeared to be the gunman’s Facebook page, where he said he would soon broadcast live video of the attack.

The 17-minute video, which appeared to be recorded on a helmet camera, shows his drive to the mosque, followed by a harrowing nearly two minutes of his firing on the worshipers in one of the mosques before fleeing the building and running back to his car and swapping weapons.

He then is seen re-entering the mosque and again begins shooting, continuing to methodically move through the mosque. Several victims can be seen in the footage, many lying on top of one another motionless in a corner of the room.

After another few minutes, he leaves again, gets in his vehicle and drives away, talking to himself throughout.

“There wasn’t even time to aim, there was so many targets,” he says at one point.

The New Zealand police said on Twitter they were “aware there is extremely distressing footage relating to the incident in Christchurch circulating online,” and urged people not to share the link. “We are working to have any footage removed,” the police said.

Facebook said it was alerted by the police shortly after the livestream started. “We quickly removed both the shooter’s Facebook and Instagram accounts and the video,” Mia Garlick, a Facebook representative, said in a statement. “We’re also removing any praise or support for the crime and the shooter or shooters as soon as we’re aware.”

a person holding a sign: “This is and will be one of New Zealand’s darkest days,” Prime Minister Jacinda Ardern said at a news conference on Friday.© Hagen Hopkins/Getty Images “This is and will be one of New Zealand’s darkest days,” Prime Minister Jacinda Ardern said at a news conference on Friday.

The video and Twitter posts showed weapons covered in the names of past military generals and men who have recently carried out mass shootings.

In his manifesto, he identified himself as a 28-year-old man born in Australia. He listed his white nationalist heroes, described what he said motivated him to attack, and said he purposely used guns to stir discord in the United States around the Second Amendment.

The website Stuff reported that the police had cleared Cathedral Square, about halfway between the two mosques, where people had gathered at a rally to fight climate change.

Aman Singh, who works at a convenience store close to the Deans Avenue mosque, said he heard gunshots and shortly afterward people streamed past, bloody and crying.

Mr. Singh, 26, said he knew several people who worshiped at the mosque.

“My really good friend goes there,” he said, adding that he had not been able to confirm the friend’s whereabouts.

Mr. Singh hid in the store with his wife while they waited for the police to arrive. He said they would be unlikely to be able to return home anyway, as their street was cordoned off because it was close to the Linwood Mosque that was also attacked.

While the details of the shooting were still unclear, one purported witness, Mohammad Isam, a Bangladeshi journalist, posted a video of members of Bangladesh’s national cricket team who he said escaped the attack.

a group of men riding on the back of a car: An injured man outside a mosque in central Christchurch, where the police reported an active shooter.© Mark Baker/Associated Press An injured man outside a mosque in central Christchurch, where the police reported an active shooter.

The video showed several men in the team’s jersey walking briskly through a park with the message, “Bangladesh team escaped from a mosque near Hagley Park where there were active shooters. They ran back through Hagley Park back to the Oval,” referring to the nearby cricket ground.

Ms. Ardern canceled her events for the rest of the day after the shooting.

The shooting happened a day after the country’s minister for climate change, James Shaw, 45, told the police that he was grabbed and hit on the street in the country’s capital, Wellington.

Reports of the shooting happened as young protesters were gathering in Christchurch and cities around the world to demand action on climate change.

Christchurch, with about 388,000 residents, is the biggest city on New Zealand’s South Island, hugging the Pacific Ocean coast.

There hasn’t been a mass shooting in New Zealand since 1990, when a man killed 13 people, including two 6-year-olds, after a dispute with his neighbor in the seaside town of Aramoana.

That shooting led to tightened gun laws, including restrictions on “military style semiautomatic weapons.”

Gun owners must be licensed, a process that includes a review of criminal activity and mental health, attendance at a safety program, an explanation of how the gun would be used, a residence visit to ensure secure storage, and testimonials from relatives and friends.

a person walking down a street next to a car: Police officers outside one of the two mosques that were attacked in Christchurch, New Zealand, on Friday.© Martin Hunter/SNPA, via Reuters Police officers outside one of the two mosques that were attacked in Christchurch, New Zealand, on Friday.

Murders are rare in New Zealand, and gun homicides even rarer. There were 35 murders countrywide in 2017. Since 2007, gun homicides have been in the single digits each year except 2009, when there were 11.

But there are plenty of guns. There were 1.2 million registered firearms in a country of 4.6 million people in 2017, according to the Small Arms Survey, a Swiss nonprofit.

Charlotte Graham-McLay reported from Wellington, New Zealand, and Austin Ramzy and Daniel Victor from Hong Kong. Megan Specia and Jason Bailey contributed reporting from New York.