Hospital staffers rushed to shelter the patients inside rooms and barricade the doors.
First responders moved to catch the gunman, put out a fire and end the threat.
And nurses and physicians risked danger to find and treat gunshot victims and move them to safety.
When it was over, two people, including the suspect, were dead and six others wounded. The gunman, Dr. Henry Bello, died of a self-inflicted gunshot wound, law enforcement officials said. He killed a physician, whom police identified as Tracy Sin Yee Tam, 32. Of the six injured, one was in critical condition, and the others were stable Saturday.
The violence could have been worse if not for the life-saving efforts of first responders and hospital employees, officials said.
"Our hospital staff responded heroically in addressing this situation," hospital spokesman Errol Schneer said.
"Many of our staff risked their own lives to save patients, and the fact that we had only one injury to a patient is truly a testament to the work that our staff did in protecting the patients at all costs."
New York Mayor Bill de Blasio paid tribute to the first responders, nurses, doctors and staff who risked their lives.
"This was a horrific situation unfolding in a place that people associate with care and comfort, a situation that came out of nowhere, but even in the midst of this horror there were many, many acts of heroism," de Blasio said.
Here are a few of those acts:
When staff members heard about the "code silver" emergency warning, they rushed into patient rooms to help them take shelter from the gunman.
Hospital staff is trained to care for patients first in shooting situations, said Patricia Cahill, the hospital's chief nursing officer.
"Those nurses took whoever they could and they put them in bathrooms with them, they huddled together and they held, they barricaded the door, they got in the safest place they could," she said.
Ruth Velazquez, an HIV counselor, said employees are ready for situations such as this.
"We locked every single door in that place, every single door was locked, and we put the patients there, we made sure that our main concern was the patients," she said.
Patients who were physically able also helped out. Krystal Rivera, 23, said she was with her hospital roommate, a visitor and a nurse when they heard gunshots.
"I barricaded the door with an IV machine, two chairs and my whole bed," Rivera said.
For hospital employee Gonzalo Corazo, keeping patients safe was his initial reaction to the emergency, he told CNN affiliate WABC-TV
"That's when I panicked, and then all of sudden my first instinct was the patients, taking care of the patients," Corazo said.
De Blasio said that "doctors and nurses, all the personnel, responded with extraordinary bravery, with cool professionalism. They protected each other, they protected their patients even amidst this horrible situation."
'God bless our first responders'
Wearing a white lab coat, the suspected shooter was able to enter the hospital and go to the 16th floor, where he used to work. Once there, he began shooting an AM-15 weapon.
During the shooting, the hospital's fire alarm system went off, apparently because the suspect tried to set himself on fire, said James O'Neill, the New York police commissioner.
Both police and firefighters quickly responded to the scene, and they eventually found the suspect dead of a self-inflicted gunshot wound on the 17th floor, O'Neill said.
"There were many things amidst this pain to be proud of today, and God bless our first responders," de Blasio said. "They put the safety of their fellow New Yorkers first, as they always do."
Sridhar Chilimuri, the hospital's physician in chief, praised the speed and professionalism of the New York police, who came into a scene of "total chaos" and established some order.
"We don't work with them every day, but it just seemed to work like a team," Chilimuri said.
Moving the patients
Once first responders put out the fire and gave the all-clear, hospital staff rushed to treat the gunshot victims on the 16th and 17th floors.
As senior officials in the hospital, Chilimuri, Cahill and others went up to the 17th floor and pulled two patients out of the dangerous scene, "literally over dead bodies and blood and gore all over," the physician in chief said.
Staffers began moving those who were seriously injured from floor to floor, picking up doctors along the way to treat the victims' gunshot wounds.
"We had trauma surgeons already in the building, we have neurosurgeons in the building, we have hand surgeons in the building, so that obviously helped us expedite the process very quickly," Chilimuri said.
"There were physicians actively working with nursing staff on every floor, so as each patient came off we took them directly to the emergency room so we could work on them quite quickly."
The speed of their efforts "absolutely" helped save lives, Chilimuri said.
"This happening in a hospital, which is well-prepared for it, we think will end up with good outcomes at the end," he said.
Ambulance EMT Robert Maldonado, who was sheltered in a locked room, told CNN affiliate WABC-TV that he and a partner heard someone yell for EMTs. They jumped out of the room and immediately helped a doctor with a gunshot wound in his stomach.
"He was in a lot of pain obviously," Maldonado to the station.
"We carried him nine floors. The police department helped carry, I helped put pressure on the wound, the entrance and exit wound, and the police department, they all just grabbed his legs, grabbed his arms."
Staff also removed patients from the 16th and 17th floors to more established safe areas. They moved between 40 to 50 patients in less than 10 minutes, Chilimuri said.
Even security guards helped with patients and assisted nurses and doctors, going "way above and beyond the call of duty," said Schneer, the hospital spokesman.