Dressed all in black with his hood pulled up, the gunman apparently took his own life as scores of police converged on the Borderline Bar & Grill in Southern California.
The motive for the rampage late Wednesday night was under investigation.
The suspected shooter, identified as 28-year-old Ian David Long, was a former machine gunner and Afghanistan war veteran who was interviewed by police at his home last spring after an episode of agitated behavior that authorities were told might be post-traumatic stress disorder.
Opening fire with a handgun with an illegal, extra-capacity magazine, Long shot a security guard outside the bar and then went in and took aim at employees and patrons, authorities said. He also used a smoke bomb, according to a law enforcement official who was not authorized to discuss the investigation publicly and spoke on condition of anonymity.
What we know about Thousand Oaks shooting suspect
The dead included a veteran sheriff's deputy who rushed in to confront the gunman, as well as a 22-year-old man who planned to join the Army, a freshman at nearby Pepperdine University and a recent Cal Lutheran graduate.
Survivors of the rampage - mostly young people who had gone out for college night at the Borderline, a hangout popular with students from nearby California Lutheran University - seemed to know what to do, having come of age in an era of active-shooter drills and deadly rampages happening with terrifying frequency.
Several of the survivors said they were also at the outdoor country music festival in Las Vegas last year when a gunman in a high-rise hotel killed 58 people.
Ventura County Sheriff Geoff Dean said in his 41 years in law enforcement, he'd never experienced a mass shooting.
"I never thought I would see the things around the country that would happen, but I've learned it doesn't matter what community you're in, it doesn't matter how safe your community is, it can happen anywhere," he said.
"Unfortunately our young people, people at nightclubs, have learned that this may happen, and they think about that," the sheriff said. "Fortunately it helped save a lot of lives that they fled the scene so rapidly."
Witness John Hodge, of Moorpark, said he saw Long throwing smoke bombs into the front of the restaurant. He also said he saw a security guard get shot.
"I was at the front door and I was talking to my stepdad. I just started hearing these big pops. Pop, pop, pop. There was probably three or four, I hit the ground. I look up - the security guard is dead. Well, I don't want to say he was dead, but he was shot.
"He was down. The gunman was throwing smoke grenades all over the place. I saw him point to the back of the cash register...and he just kept firing. I ran out the front door," he said.
Hero saves as many as 30 people during California nightclub shooting
He added that there may have been about 12 shots by the time he got out of the door.
"I thought it was a joke when the shots started firing. I know people there. I hope everybody's OK. I don't know how I didn't get shot," the man said.
Matt Wennerstrom said he pulled people behind a pool table, and he and friends shielded women with their bodies after hearing the shots. When the gunman paused to reload, Wennerstrom said, he used a barstool to shatter a window and then helped about 30 people escape. He heard another volley of shots after they got out.
"All I wanted to do was get as many people out of there as possible," he told KABC-TV. "I know where I'm going if I die, so I was not worried."
Jason Coffman received the news that his son Cody, 22, who was about to join the Army, was dead. Coffman broke down as he told reporters how his last words to his son as he went out that night were not to drink and drive and that he loved him.
"Oh, Cody, I love you, son," Coffman sobbed.
We have sent patrol and SWAT resources to assist with the incident in Thousand Oaks. We will continue to monitor and provide whatever resources are requested. Please keep the victims in your thoughts & prayers.— Oxnard Police Dept. (@OxnardPD1903) November 8, 2018
It was the nation's deadliest such attack since 17 students and teachers were killed at a Parkland, Florida, high school nine months ago. It also came less than two weeks after a gunman massacred 11 people at a synagogue in Pittsburgh.
Authorities searched Long's home in Newbury Park, about 5 miles from the Borderline bar, for clues to what set him off.
"There's no indication that he targeted the employees. We haven't found any correlation," the sheriff said. "Maybe there was a motive for this particular night, but we have no information leading to that at all."
The country bar is located near the 101 Freeway off Moorpark Road.
The website for Borderline showed that Wednesday nights are College Country Nights that last from 9 p.m. until 2 a.m. Colleges near the club are California Lutheran University and California State University, Channel Islands, as well as Moorpark, Ventura and Oxnard College.
Students from Pepperdine University in Malibu also often attend College Country Nights, and there may have been some at the bar Wednesday night. It was unclear how many, according to the university.