Students said the gunman, identified by law enforcement as Dimitrios Pagourtzis, first opened fire in an art class at Santa Fe High School shortly before 8 a.m. CT (1300 GMT) on Friday. Students and staff fled after seeing classmates wounded and a fire alarm triggered a full evacuation.
Ten people were also wounded in the attack, according to Texas Governor Greg Abbott, who said the suspect left behind explosive devices.
Abbott said investigators had seen a T-shirt on the suspect's Facebook page that read "Born to Kill," and authorities were examining his journal. But there were no outward signs he had been planning an attack, the governor said.
"Here, the red flag warnings were either non-existent or very imperceptible," Abbott told a news conference.
Classmates described the Santa Fe suspect as a quiet student who kept to himself and played on the football team. They also said he wore a trench coat to school on the day of shooting, when temperatures topped 90 degrees Fahrenheit (32 degrees Celsius).
It was the latest in a long series of deadly shootings at U.S. schools. Seventeen teens and educators were shot dead at a Parkland, Florida, high school on Feb. 14, a massacre that stirred the nation's long-running debate over gun ownership.
It was the second mass shooting in Texas in less than a year, following a massacre at a rural church in November, when a man armed with an assault rifle shot dead 26 people.
The Galveston County Sheriff's Office said Pagourtzis was being charged with capital murder, adding in a post on its Facebook page that more charges could follow.
Abbott also said the suspect in the fourth-deadliest mass shooting at a U.S. public school had used firearms taken from his father.
"Not only did he want to commit the shooting, but he wanted to commit suicide after the shooting," Abbott said, citing a police review of the suspect's journals. "He didn't have the courage to commit suicide."
Two other people are in custody, Abbott said.
Investigators are talking to the suspect, said Steven McCraw, director of the Texas Department of Public Safety.
Explosive devices were also found at the school, located about 30 miles (50 km) southeast of Houston, and off campus, Harris County Sheriff Ed Gonzalez tweeted.
Police searched two homes and a vehicle linked to the suspect, where they have found multiple homemade explosive devices, Abbott said.
"There is no reason to suspect that any other person, other than the shooter, made those devices," Abbott said.
"I wanted to take care of my friends, but I knew I had to get out of there," Marshall said, adding that she saw at least one person hit. "I knew the guy behind me was dead."
Orlando Gonzalez said his 16-year-old son Keaton fled the attack, but one of his friends was shot and wounded.
"I was really worried, I didn't know what was going on ... I almost couldn't drive," Gonzalez said. "I just imagine what he's going through ... He's still scared."
The school has some 1,462 students, according to federal education data.
Among those wounded in the attack was Santa Fe Independent School District police officer John Barnes, one of two school officers who engaged the suspect. Barnes was in critical condition after a gunshot wound to his elbow that almost caused him to bleed out, hospital officials said.
U.S. President Donald Trump called the latest school massacre "absolutely horrific."
"My administration is determined to do everything in our power to protect our students, secure our schools and to keep weapons out of the hands of those who pose a threat to themselves and to others," Trump said at the White House.
Days after the Parkland shooting, Trump said that elected officials should be ready to "fight" the powerful National Rifle Association lobby group. Early this month he embraced that group, telling its annual meeting in Dallas "your Second Amendment rights are under siege."
The Second Amendment of the U.S. Constitution protects the right to bear arms.
No major federal gun controls have been imposed since Parkland, though the administration is pursuing a proposed regulatory ban on "bump stocks," which enable a semi-automatic rifle to fire a steady stream of bullets. The devices were used in an October 2017 mass shooting in Las Vegas that killed 59 people but have not played a role in other major U.S. mass shootings.
The first mass U.S. shooting of the modern age took place in Texas in 1966 when a sniper perched in a University of Texas clock tower unleashed 90 minutes of terror in an attack that killed 16 people and wounded 30.
(Additional reporting by Ernest Scheyder and Liz Hampton in Houston, Gina Cherelus and Peter Szekely in New York and Mark Hosenball and Ian Simpson in Washington; Writing by Daniel Wallis, Scott Malone and Jon Herskovitz; Editing by Bill Tarrant and Tom Brown)
(Except for the headline, this story has not been edited by NDTV staff and is published from a syndicated feed.)
Election Results for Lok Sabha Election 2019 will be out on May 23. Get the latest election news and live updates on ndtv.com/elections. Catch all the action on NDTV Live. Like us on Facebook or follow us on Twitter and Instagram for news updates from each of the 543 parliamentary seats for the election 2019