The 18-year-old suspect, who was killed apparently by police, also had shot his own grandmother before fleeing from the scene, then crashing his getaway car and launching a bloody rampage at Robb Elementary School in the town of Uvalde, Texas, about 80 miles (130 km) west of San Antonio.
The motive was not immediately clear.
Law enforcement officers saw the gunman, clad in body armor, emerge from his crashed vehicle carrying a rifle and "engaged" the suspect, who nevertheless managed to charge into the school and open fire, Texas Department of Public Safety (DPS) Sergeant Erick Estrada said on CNN.
Speaking from the White House hours later, a visibly shaken US President Joe Biden urged Americans to stand up to the politically powerful US gun lobby, which he blamed for blocking enactment of tougher "common-sense" firearms safety laws.
Biden ordered flags flown at half-staff daily until sunset on Saturday in observance of the tragedy.
Governor Greg Abbott said that the suspect, identified as Salvador Ramos, was apparently killed by police officers, and that two officers were struck by gunfire, though the governor said their injuries were not serious.
Authorities said the suspect acted alone.
After confusing early accounts of the death toll, the state attorney general's office in an official statement put the tally of lives lost at 18 children and two adults, including the gunman. A Texas DPS spokesperson later told CNN that 19 school children and two adults were killed, not counting the shooter.
The school's student body consists of children in the second, third and fourth grades, according to Pete Arredondo, chief of the Uvalde Consolidated Independent School District Police Department, who also addressed reporters. Pupils in those grades would likely have ranged in age from 7 to 10.
The carnage unfolded 10 days after 10 people were killed in Buffalo, New York, in a predominantly Black neighbourhood. Authorities have charged an 18-year-old man who they said had travelled hundreds of miles to Buffalo and opened fire with an assault-style rifle at a grocery store.
Tuesday's bloodshed in Texas began when the suspect shot his grandmother before going to the school, Texas Department of Public Safety officer Chris Olivarez said on Fox News, a development Abbott mentioned earlier in the day.
"I have no further information about the connection between those two shootings," the governor said.
University Hospital in San Antonio said on Twitter that it had received two patients from the shooting in Uvalde, a 66-year-old woman and a 10-year-old girl, both listed in critical condition.
Uvalde Memorial Hospital said 15 students from Robb Elementary were treated in its emergency room, with two transferred to San Antonio for further care, while a third patient transfer was pending. It was not immediately clear whether all of those students survived.
A 45-year-old victim grazed by a bullet was also hospitalised at Uvalde Memorial, the hospital said.
Hours after the shooting, police had cordoned off the school with yellow tape. Police cruisers and emergency vehicles were scattered around the perimeter of the school grounds. Uniformed personnel stood in small clusters, some in camouflage carrying semi-automatic weapons.
EPIDEMIC OF GUN VIOLENCE
The rampage was the latest in a series of mass school shootings that have fuelled a fierce debate between advocates of tighter gun controls and those who oppose any legislation that could compromise the constitutional right of Americans to bear arms.
The shooting in Texas was one of the deadliest at a US school since a gunman killed 26 people, including 20 children, at Sandy Hook Elementary School in Connecticut in December 2012. In 2018, a former student at Marjory Stoneman Douglas High School in Parkland, Florida, killed 17 students and educators.
Firearms became the leading cause of death for US children and adolescents in 2020, surpassing motor vehicle accidents, according to a University of Michigan research letter published in the New England Journal of Medicine last month.
The day's horrors were reflected on the Facebook page of Robb Elementary School.
Earlier this week, its posts showed the usual student activities - a trip to the zoo for second-graders and a save-the-date for a gifted-and-talented showcase. But on Tuesday, a note was posted at 11:43 am: "Please know at this time Robb Elementary is under a Lockdown Status due to gunshots in the area. The students and staff are safe in the building."
A second post was more explicit: "There is an active shooter at Robb Elementary. Law enforcement is on site." Administrators asked parents to stay away. And finally, a note was posted advising parents that they could meet their children at the small city's civic center.