The shooter, who was equipped with a gas mask and smoke grenades, set off a fire alarm to draw students out of their classrooms shortly before the school day ended, officials said.
Authorities offered no immediate details on the 19-year-old suspect or any possible motive, except to say that he had been expelled for disciplinary reasons.
Frantic parents rushed to Marjory Stoneman Douglas High School to find SWAT team members and ambulances surrounding the campus. Live footage showed emergency workers who appeared to be treating the wounded on sidewalks.
"It is a horrific situation," said Robert Runcie, superintendent of the school district in Parkland, about an hour's drive north of Miami. "It is a horrible day for us."
WATCH: Gov. Rick Scott says violence is not tolerated in Florida
The suspect was taken into custody without a fight about an hour after he left the scene. He had at least one rifle and multiple magazines, authorities said.
"It's catastrophic. There really are no words," Broward County Sheriff Scott Israel said on Twitter.
The attacker used the fire alarm "so the kids would come pouring out of the classrooms into the hall," Sen. Bill Nelson said in an interview on CNN.
"And there the carnage began," said Nelson, who said he was briefed by the FBI.
PHOTOS: Gunshots ring out at school in Florida
The Florida Democrat said he did not know if the gunman used the smoke grenades, but he assumed that's why he had a gas mask on.
Most of the fatalities were inside the building, though some victims were found fatally shot outside, the sheriff said.
The gunman was identified as Nikolas Cruz by a U.S. official briefed on the investigation. The official spoke on condition of anonymity because he was not authorized to discuss the information publicly.
Freshman Max Charles said he heard five gunshots.
"We were in the corner, away from the windows," he said. "The teacher locked the door and turned off the light. I thought maybe I could die or something."
As he was leaving the building, he saw four dead students and one dead teacher. He said he was relieved when I finally found his mother.
"I was happy that I was alive," Max said. "She was crying when she saw me."
#BSO is working a developing incident regarding a report of active shooter located at 5901 Pine Island Rd, Parkland. Here's what we know so far: deputies are responding to reports of a shooting at Stoneman Douglas High. There are reports of victims. PIO will be on scene 3:15pm.— Broward Sheriff (@browardsheriff) February 14, 2018
Avoid the area of Stoneman Douglas HS. #BSO is currently working a developing incident regarding a report of active shooter.— Broward Sheriff (@browardsheriff) February 14, 2018
In a cul-de-sac near the school, Michael Nembhard was sitting in his garage when he saw a young man in a burgundy shirt walking down the street. In an instant, a police cruiser pulled up, and officers jumped out with guns drawn.
"All I heard was 'Get on the ground! Get on the ground!'" Nembhard said. He said he could not see the suspect's face, but that the man got on the ground without incident.
The day started normally at the school, which had a morning fire drill. Students were in class around 2:30 p.m. when another alarm sounded.
Noah Parness, a 17-year-old junior, said he and the other students calmly went outside to their fire-drill areas when he suddenly heard popping sounds.
"We saw a bunch of teachers running down the stairway, and then everybody shifted and broke into a sprint," Parness said. "I hopped a fence."
Beth Feingold said her daughter, Brittani, sent a text at 2:32 p.m. that said, "We're on code red. I'm fine," but sent another text shortly afterward saying, "Mom, I'm so scared."
Brittani later was able to escape the school, which is one of the largest in the state, with about 3,000 students.
Inside, students heard loud bangs as the shooter fired. Many of them hid under desks or in closets and barricaded doors.
Television footage showed students leaving in a single-file line with their hands over their heads as officers urged them to evacuate quickly.
Caesar Figueroa said when he got to the school to check on his 16-year-old daughter, he saw police officers drawing machine guns as they approached the campus.
"My wife called me that there was an active shooter, and the school was on lockdown. I got on the road and saw helicopters. ... It was crazy and my daughter wasn't answering her phone." She finally texted him that she was inside a closet with friends.
Len Murray's 17-year-old son, a junior at the school, sent his parents a chilling text: "Mom and Dad, there have been shots fired on campus at school. There are police sirens outside. I'm in the auditorium and the doors are locked."
A few minutes later, he texted again: "I'm fine."
Murray said he raced to the school only to be stopped by authorities under a highway overpass within view of the school buildings. He said he told his son to save his battery and stop texting. The boy's mother told him to turn off his ringer.
Murray said he's had just one thought running through his mind since he got his son's text: "All I keep thinking about is when I dropped him off this morning. I usually say, 'I love you,' and I didn't this morning. He's 17, he's at that age, and I didn't say it this morning, and I'm just kicking myself right now over and over and over. Say it early and often, I'm telling you."
The school was to be closed for the rest of the week.
The Newtown attack shocked even a country numbed by the regularity of school shootings. The Dec. 14, 2012, assault at Sandy Hook Elementary School killed 26 people - 20 first-graders and six staff members.
The 20-year-old gunman, who had also fatally shot his mother in her bed, then killed himself.
My prayers and condolences to the families of the victims of the terrible Florida shooting. No child, teacher or anyone else should ever feel unsafe in an American school.— Donald J. Trump (@realDonaldTrump) February 14, 2018
Just spoke with @POTUS about shooting at Marjory Stoneman Douglas High School. My thoughts and prayers are with the students, their families and the entire community. We will continue to receive briefings from law enforcement and issue updates.— Rick Scott (@FLGovScott) February 14, 2018
MORE: First lady Melania Trump aware of Florida school shooting, her spokesperson says, sends thoughts and prayers to those involved. https://t.co/BuEAqGjCN1— ABC News (@ABC) February 14, 2018
Praying for everyone at Marjory Stoneman Douglas High School. Just spoke with Broward Undersheriff to ensure they have everything they need. And just spoke to FBI to make sure all federal resources are being made available to help. Will continue to monitor closely.— Bill Nelson (@SenBillNelson) February 14, 2018