Videos uploaded to social media showed chaotic scenes. (Reuters)

A 19-year-old has been identified as the gunman in the latest mass shooting in the United States, a rampage which left three dead including two children and 12 other people injured.

Authorities said Santino William Legan was armed with an AK47-type assault rifle when he opened fire at random at the Gilroy Garlic Festival Sunday in northern California before being gunned down by police within one minute of the shooting.

Law enforcement officials said Monday they have not established a motive for the killings. But media reports said investigators were poring over Legan's social media accounts, including an Instagram account created four days ago in which he referenced a white supremacist book.

One message on the account posted shortly before the attack included a picture of people at the festival with the words "Ayyy garlic festival time Come get wasted on overpriced sh**," media reports said.

A second message featured a photo of Smokey the Bear with a sign that reads "Fire Danger High Today" alongside a reference to the white supremacist book "Right is Might" which promotes anarchy.

Gilroy police chief Scot Smithee said investigators were trying to confirm witness accounts of a second suspect who may have assisted Legan.

"We had reports on a potential second suspect but we don't have confirmation that he fired and we are trying to determine who it is and what his role was," Smithee told reporters at a press conference.

The three fatalities include a six-year-old boy who was attending the festival with his mother and grandmother, who were wounded in the attack. The others were identified as a 13-year-old girl and a male in his 20s.

Those wounded include one person in critical condition and range in age from 12 to 69, local media said.

The killings marked the latest chapter of America's epidemic of gun violence, which has left nearly 300 people dead in mass shootings so far this year, according to Mass Shooting Tracker.

Police said Legan managed to get into the annual outdoor festival -- which attracts nearly 100,000 and is billed as the "world's greatest summer food festival" -- by cutting through a fence to avoid security measures, including metal detectors, that were in place.

The three-day festival held in this town 80 miles (130 kilometers) southeast of San Francisco was winding down when the shots rang out, sending festival goers scattering for cover.

'Tough road ahead'

President Donald Trump paid tribute to the victims on Monday at an event at the White House.

"While families were spending time at a local festival, a wicked murderer opened fire and killed three innocent citizens including a young child," Trump said.

"We grieve for their families and ask that God comfort them."

House speaker Nancy Pelosi, whose district covers most of San Francisco, spoke of the survivors' "tough road ahead" and called for action to end gun violence.

"Every day the Senate refuses to act is a stain on the conscience of our nation," she said.

Witnesses said Legan appeared to fire randomly as he began his assault and when asked by someone "why are you doing this?" had replied: "Because I'm really angry."

"I could see him shooting in just every direction. He wasn't aiming at anyone specifically. It was just left to right, right to left," Julissa Contreras said, according to NBC.

"He definitely was prepared for what he was doing," she said.

Mass shootings are a frequent occurrence in the United States, but despite the scale of the gun violence problem, efforts to address it legislatively have largely stalled at the federal level.

Authorities said Legan has purchased his weapon legally in Nevada on July 9, thus skirting legislation in California which prevents anyone under the age of 21 from purchasing rifles or shotguns.