The teen gunman behind the mass shooting at California’s Gilroy Garlic Festival used a legally purchased “AK-47-style” rifle to slaughter his victims, who included a 6-year-old boy and 13-year-old girl left dead, authorities said Monday.

Santino William Legan, 19, cut through a metal fence and stormed into the packed annual food festival around 5:40 p.m. local time Sunday and fired indiscriminately into the crowd, according to cops.

“When the call came in, shots being fired, the closest team of officers responded immediately,” said Gilroy Police Chief Scot Smithee in a Monday press briefing. “As soon as he saw the officers, he engaged the officers and fired at the officers with that rifle.”

Smithee said that Legan legally bought the assault-style rifle on July 9 in Nevada, where the Gilroy native was living with family.

“Despite the fact that they were outgunned with their handguns against a rifle, those three officers were able to fatally wound that suspect and the event ended very quickly,” said Smithee.

“I can’t tell you how proud I am of the officers for being able to engage this guy as quickly as they did,” said Smithee. “We had thousands of people there in a very small area. It could have gone so much worse so fast.

“There absolutely would have been more bloodshed.”

The tense encounter took only about a minute — but it was all the time Legan needed to kill three people.

They included the 6-year-old boy — identified by his family as Stephen Romero — the 13-year-old girl and a man in his 20s, said Smithee.

“Any time a life is lost, it’s a tragedy,” said Smithee, his voice quivering with emotion. “When it’s young people, it’s even worse.”

Another 12 people were injured, including at least one in critical condition, authorities said.

Officials are yet to identify a motive behind the bloodshed, but vowed a thorough investigation — including into unconfirmed witness accounts of an accomplice.

“We are certainly investigating all leads to try to determine who that potential second suspect is and what their potential role is,” said Smithee.

As for Legan, investigators from both local and federal agencies are reviewing body-camera footage and social media, as well as interviewing friends and family, in an effort to get inside the killer’s mind.

“We don’t have a motive for the shooting,” said Smithee, shortly after identifying Legan to reporters by name for the first time. “I say that name with some hesitation because I don’t believe someone like this deserves notoriety.”