Police said a white 18-year-old man in military gear used a helmet camera to livestream the attack in Buffalo, New York state, on mostly black shoppers and workers on Saturday.
For at least two minutes, he broadcast the shooting live on the streaming platform Twitch before the service ended his transmission.
Police said he shot 11 black victims and two who were white before surrendering to police. Later, he appeared before a judge in a paper medical gown and was arraigned on murder charges.
The suspected gunman was identified as Payton Gendron, of Conklin, about 200 miles south-east of Buffalo.
Speaking near the scene of the attack, New York governor Kathy Hochul said: “It is my sincere hope that this individual, this white supremacist who just perpetrated a hate crime on an innocent community, will spend the rest of his days behind bars. And heaven help him in the next world as well.”
It was not immediately clear why Gendron travelled to Buffalo to stage the assault. A clip apparently from his Twitch feed, posted on social media, showed him arriving at the supermarket in his car.
Buffalo Police Commissioner Joseph Gramaglia said the gunman shot four people outside the store, three fatally.
Inside, security guard Aaron Salter, who was a retired Buffalo police officer, fired multiple shots. A bullet hit the gunman but he was wearing a bulletproof vest and it had no effect, the commissioner added.
The gunman then killed the guard before stalking through the store shooting other victims. Upon being confronted by police, the suspect had put the gun to his own neck before officers talked him into dropping the weapon.
Also among the dead was Ruth Whitfield, the 86-year-old mother of retired Buffalo fire commissioner Garnell Whitfield.
He told the Buffalo News:“My mother was a mother to the motherless. She was a blessing to all of us.”
A source said investigators are looking into whether Gendron had posted a manifesto online.
Buffalo police declined to comment on the document, circulated widely online, that purports to outline the attacker’s racist, anti-immigrant and antisemitic beliefs, including a desire to drive all people not of European descent from the US.
It said he drew inspiration from the man who killed 51 people at two mosques in Christchurch, New Zealand, in 2019.
Buffalo Mayor Byron Brown told a news conference: “This is the worst nightmare that any community can face, and we are hurting and we are seething right now.
“The depth of pain that families are feeling and that all of us are feeling right now cannot even be explained.”
At an earlier news briefing, Erie County Sheriff John Garcia pointedly called the shooting a hate crime.
“This was pure evil,” he said. “It was a straight up racially motivated hate crime from somebody outside of our community, outside of the city of good neighbours… coming into our community and trying to inflict that evil upon us.”
US President Joe Biden said in a statement that he and the First Lady were praying for the victims and their families.
He said: “We still need to learn more about the motivation for today’s shooting as law enforcement does its work, but we don’t need anything else to state a clear moral truth: A racially motivated hate crime is abhorrent to the very fabric of this nation.
“Any act of domestic terrorism, including an act perpetrated in the name of a repugnant white nationalist ideology, is antithetical to everything we stand for in America.”
Tops Friendly Markets released a statement saying: “We are shocked and deeply saddened by this senseless act of violence and our thoughts and prayers are with the victims and their families.”