An 18-year-old man wearing military gear was arrested after a stand-off at the scene in the city of Buffalo.
The suspect entered a busy supermarket on Saturday afternoon before opening fire. He used a camera to stream the attack online, police said.
US President Joe Biden condemned what he called an “abhorrent” attack.
“We are investigating this incident as both a hate crime and a case of racially-motivated violent extremism,” Stephen Belongia, the agent in charge of the FBI’s Buffalo office, told a news conference.
The suspect is believed to have driven for several hours to reach the predominantly black area of the city. Thirteen people were shot in total and the majority of the victims were black, Buffalo Police Commissioner Joseph Gramaglia said.
Speaking to CBS, a police source alleged that the man had shouted racial slurs during the shooting.
“He exited his vehicle. He was very heavily armed. He had tactical gear. He had a tactical helmet on. He had a camera that he was live-streaming what he was doing,” Mr Gramaglia told reporters.
He said the suspect surrendered his weapon after a tense stand-off and was taken into custody.
The three wounded victims – who all worked at the supermarket – have not sustained life-threatening injuries.
A retired police officer who was working as a security guard tried to shoot the suspect but was among those killed, police said.
The man has since been charged in court with first-degree murder, and Erie County District Attorney John Flynn said further charges may still be brought.
“This is the worst nightmare that any community can face and we are hurting, we are seething right now,” Buffalo Mayor Byron Brown told reporters.
“We cannot let this hateful person divide our community or our country,” he added.
Grady Lewis, who witnessed the attack from across the street, told local media that he saw the man open fire. “I saw the guy go in, army-style, bent over, just shooting at people,” he said.
Shonnell Harris, who was working in the shop during the attack, told Buffalo News that she heard more than 70 shots as she ran to escape the building through a backdoor.
“The store was full. It was the weekend,” she said. “It feels like a nightmare.”
Describing the aftermath of the attack, one police officer told Buffalo News: “It’s like walking onto a horror movie, but everything is real. It is Armageddon-like”.
Later on Saturday, New York Governor Kathy Hochul said the suspect was a “white supremacist who has engaged in an act of terrorism”.
This was “a military style execution targeting people who simply wanted to buy groceries in a neighbourhood store,” she said while visiting Buffalo.
In a statement, US President Joe Biden said he and the First Lady were praying for the victims and their families.
“A racially-motivated hate crime is abhorrent to the very fabric of this nation,” he said. “We must do everything in our power to end hate-fuelled domestic terrorism.”