Police responded to Bakersfield Heart Hospital after receiving reports of an active shooter.
Assistant Chief of Police Greg Terry said the suspect first fired several shots at a glbad door in the back of the building and then entered the hospital with a gun and an badault rifle.
The man was confronted by a security officer, but they did not shoot.
Soon after, the suspect left the hospital and was shot by the police, Terry said.
The suspect, who was not identified, was being treated at a local hospital on Friday night and was in stable condition, authorities said.
There were no patients, staff or visitors injured during the incident, police said.
The gunman's motive remains unclear, but authorities believe that terrorism was not a factor in the shooting, said the FBI's special supervision agent, José Moreno.
The hospital locked itself after a housekeeper alerted administrators to a person with a gun, said the hospital's general director, Michelle Oxford.
Ronnie Nop and her 8-year-old son were visiting a family member in the hospital's intensive care unit when he was locked up. While taking refuge in an empty room, Nop said he saw the gunman running out of the building.
This story has been updated to correct the suspect's condition. He was hospitalized on Friday night and is in stable condition, authorities said.
Jennifer Selva of CNN contributed to this report.