“I am in shock,” she said softly after clutching her mother, Cheryl Kaiser, on the sidewalk outside the school.

Seventeen patients were treated in three area hospitals, including two who died, said Dr. Evan Boyar of the Broward Health System. All suffered gunshot wounds.

“Words cannot express the sorrow that we feel,” said Robert W. Runcie, the Broward schools superintendent. “No parent should ever have to send their kid to school and have them not return.”

Parkland, an affluent suburb of Fort Lauderdale with a population of about 30,000, is known for its good public schools. Stoneman Douglas High is among the largest in the Broward school district, with about 3,000 students. The school will remain closed for the rest of the week. Gov. Rick Scott directed the state to lower its flags at half-staff until Monday.

“My prayers and condolences to the families of the victims of the terrible Florida shooting,” President Trump wrote on Twitter. “No child, teacher or anyone else should ever feel unsafe in an American school.”

As the authorities frantically searched for the person responsible, they asked residents of the city to avoid the area around the school.

For hours, parents were lined along Coral Springs Drive, calling their children on cellphones and pacing. Some parents said their children told them only to text to not make noise. One parent of two daughters at a nearby middle school said he sat in a bank lobby near the school and prayed.

The gunfire came as some students were still staring at chalkboards and listening to lectures.

Rebecca Bogart, 17, a senior, said her teacher was finishing up a discussion of the Holocaust when she heard a series of loud bangs.

“We all got on the floor and under the desk,” said Ms. Bogart, who was still shaking outside the school. “It felt like we were there 10 or 15 minutes and then shots came through the window and the glass shattered.”

She couldn’t see her classmates fall, but she could see at least five were bleeding, one in the head and one in the leg. “I was trying to keep calm and my friend was holding my hand to keep it from shaking,” she said.

When the authorities arrived, they took out her wounded classmates first. “There was blood all over the floor,” she said, “You never think something like this is going to happen to you and then it does.”