During the shooting on Tuesday, police circled Robb Elementary School in Uvalde, Texas, breaking windows in an effort to evacuate children and staff, Texas Department of Public Safety spokesperson Chris Olivarez told CNN.
Officers eventually breached the classroom and killed the gunman, identified as 18-year-old Salvador Ramos.
Ramos began his rampage by shooting his grandmother at the home where he lived with his grandparents. He then drove to the nearby school where he crashed his car and entered the building wearing tactical gear and carrying a rifle, authorities said.
His grandmother survived but is in critical condition, and investigators hope she can shed light on a motive for the shooting. Multiple children were also injured, although authorities have not provided an exact tally.
Ramos purchased two rifles and hundreds of rounds of ammunition days before the attack, CNN reported, citing a state senator who had been briefed by law enforcement.
The attack, which came 10 days after an avowed white supremacist shot 13 people at a supermarket in a mostly Black neighbourhood of Buffalo, prompted President Joe Biden to call for stricter gun safety laws in a prime-time address to the American people.
"As a nation, we have to ask when in God's name we're going to stand up to the gun lobby," he said, his voice rising.
But new legislation appeared unlikely to pass in Washington. Virtually all Republicans in Congress oppose new gun restrictions, citing the US Constitution's guarantee of a right to bear arms, and there was no sign the massacre would alter that position.
White House officials were planning a trip to Texas for Biden, a senior administration official said.
World leaders expressed shock and sympathy. Pope Francis on Wednesday said he was "heartbroken" and called for an end to "the indiscriminate trafficking of weapons."
'Guns flow in this country like water' - Senator
The Texas rampage stands as the deadliest school shooting since a gunman killed 26 people, including 20 children, at Sandy Hook Elementary School in Connecticut in December 2012.
Uvalde, a community deep in the state's Hill Country region about 80 miles (130 km) west of San Antonio, has about 16,000 residents, nearly 80 percent of them Hispanic or Latino, according to US Census data. The town was quiet on Wednesday morning aside from a heavy law enforcement and media presence.
Community members set up fundraisers for the families of the victims to cover funeral costs, while some relatives mourned their losses on social media.
"My little love is now flying high with the angels above," Angel Garza, whose daughter, Amerie Jo Garza, was killed, wrote on Facebook. "Please don't take a second for granted. Hug your family. Tell them you love them."
The two staff members killed were identified as Eva Mireles and Irma Garcia, fourth-grade teachers trapped in the classroom with their students when the shooting began.
"My best friend, my twin was taken from me," Mireles' daughter, Adalynn Ruiz, wrote on Facebook. "Thank you for loving me in the best ways and for raising me to become so strong. Everyone who knows you knows how outgoing and funny you were and I will miss your laugh forever."
Democrats in Washington renewed calls for stronger gun safety laws. US Senator Chris Murphy of Connecticut, a leading advocate on the issue, told reporters: "You know, guns flow in this country like water. And that's why we have mass shooting after mass shooting."
The Democratic-controlled US House of Representatives last year passed two bills expanding background checks on firearm purchases. But the legislation has not advanced in the Senate, where at least 10 Republican votes are needed.
In the wake of the shooting, some Republicans called for beefing up security at schools and arming teachers, an approach opposed by gun control advocates.
"There's certainly more we can do to protect the schools by providing law enforcement," Texas Attorney General Ken Paxton told Fox News on Wednesday.
The shooting took place days before the National Rifle Association, the gun industry's main lobbying group, was to hold its annual meeting in Houston. Several prominent Texas Republicans, including Governor Greg Abbott and U.S. senators Ted Cruz and John Cornyn, were scheduled to speak to attendees.
The nonprofit Gun Violence Archive, which tracks mass shootings, has counted more than 200 so far this year, defined as those in which four or more people were killed or injured.
In Texas, which has some of the country's most permissive gun laws, a gunman killed 26 people at a church in Sutherland Springs in 2017. A mass shooting killed 10 people at a Houston-area high school the next year, and 23 people died at a Walmart in El Paso in a 2019 attack motivated by racial hatred.