Prime Minister Jacinda Ardern has condemned the act as terrorism.
A gunman broadcast live footage on Facebook of the attack on one mosque in the city of Christchurch, mirroring the carnage played out in video games, after publishing a "manifesto" in which he denounced immigrants, calling them "invaders."
New Zealand was placed on its highest security threat level, Ardern said, adding that four people taken into custody held extremist views but had not been on any police watchlists.
"It is clear that this can now only be described as a terrorist attack," Ardern said, call this "one of New Zealand's darkest days."
Police said later three people were in custody and one man in his late 20s had been charged with murder. He will appear in court on Saturday.
The visiting Bangladesh cricket team was arriving for prayers at one of the mosques when the shooting started but all members were safe, a team coach told Reuters.
Police Commissioner Mike Bush said 49 people had been killed in total.
The video footage widely circulated on social media, apparently taken by a gunman and posted online live as the attack unfolded, showed him driving to one mosque, entering it and shooting randomly at people inside.
Worshippers, possibly dead or wounded, lay huddled on the floor, the video showed. Reuters was unable to confirm the authenticity of the footage.
One man who said he was at the Al Noor mosque told media the gunman was white, blond and wearing a helmet and a bulletproof vest. The man burst into the mosque as worshippers were kneeling for prayers.
"He had a big gun ... he came and started shooting everyone in the mosque, everywhere," said the man, Ahmad Al-Mahmoud. He said he and others escaped by breaking through a glass door.
Forty-one people were killed at the Al Noor mosque, seven at a mosque in the Linwood neighbourhood and one died in hospital, police said. Hospitals said children were among the victims.
Shortly before the attack began, an anonymous post on the discussion site 8chan, known for a wide range of content including hate speech, said the writer was going to "carry out an attack against the invaders" and included links to a Facebook live stream, in which the shooting appeared, and a manifesto.
The manifesto cited "white genocide," a term typically used by racist groups to refer to immigration and the growth of minority populations, as his motivation.
The Facebook link directed users to the page of a user called brenton.tarrant.9.
A Twitter account with the handle @brentontarrant posted on Wednesday images of a rifle and other military gear decorated with names and messages connected to white nationalism. What looked like the same weapons appeared in the livestream of the mosque attack on Friday.
Darkest day
It was not immediately clear if the attacks at the two mosques were carried out by the same man.
Australian Prime Minister Scott Morrison said one of the men in custody was Australian.
All mosques in New Zealand had been asked to shut their doors and armed guards posted at them, police said, adding they were not actively looking for any other "identified suspects."
Political and Islamic leaders across Asia and the Middle East condemned the killings.
"I blame these increasing terror attacks on the current Islamophobia post-9/11," Pakistan's prime minister, Imran Khan, posted on social media. "1.3 billion Muslims have collectively been blamed for any act of terror."
Al-Azhar University, Egypt's 1000-year-old seat of Sunni Islamic learning, said the attacks had "violated the sanctity of the houses of God."
"We warn the attack is a dangerous indicator of the dire consequences of escalating hate speech, xenophobia, and the spread of Islamophobia."
Six Indonesians had been inside one of the mosques, with three managing to escape and three unaccounted for, its foreign minister said.
Afghanistan's ambassador said on Twitter three Afghans had been wounded. Two Malaysians were wounded, their foreign ministry said.
Muslims account for just over 1 percent of New Zealand's population, a 2013 census showed.
'Firing went on and on'
The online footage, which appeared to have been captured on a camera strapped to a gunman's head, showed him driving as music played in his vehicle. After parking, he took two guns and walked a short distance to the mosque where he opened fire.
Over the course of five minutes, he repeatedly shot worshippers, leaving more than a dozen bodies in one room alone. He returned to the car during that period to change guns, and went back to the mosque to shoot anyone showing signs of life.
One man, with blood still on his shirt, said in a television interview that he hid from a gunman under a bench and prayed that he would run out of bullets.
"I was just praying to God and hoping our God, please, let this guy stop" Mahmood Nazeer told TVNZ.
"The firing went on and on. One person with us had a bullet in her arm. When the firing stopped, I looked over the fence, there was one guy, changing his gun."
The video shows the gunman then driving off at high speed and firing from his car. Another video, taken by someone else, showed police apprehending a gunman on a pavement by a road.
Police said improvised explosive devices were found. The gunman's video had shown red petrol canisters in the back of his car, along with weapons.
The Bangladesh cricket team is in Christchurch to play New Zealand in a third cricket test starting on Saturday.
"They were on the bus, which was just pulling up to the mosque when the shooting begun," Mario Villavarayen, a team coach, told Reuters in a message. "They are shaken but good."
The third cricket test was cancelled, New Zealand Cricket said later.
Violent crime is rare in New Zealand and police do not usually carry guns.
Before Friday, New Zealand's worst mass shooting was in 1990 when a gun-mad loner killed 13 men, women and children in a 24-hour rampage in the tiny seaside village of Aramoana. He was killed by police.