A man who had faced charges stemming from a domestic violence arrest in Pennsylvania entered a court building Wednesday afternoon and shot four people, including a police officer, before another officer shot and killed him, authorities said.

The gunman was not immediately identified Wednesday. Those wounded in the shooting shortly after 2 p.m. at a municipal building in Masontown were taken to a hospital and are expected to survive, officials said.

The gunman was identified by state police on Thursday as Patrick S. Dowdell. Court records say he was 61 years old.

Fayette County District Attorney Richard Bower said the gunman walked up to the building, which houses the office of District Magistrate Daniel Shimshock, and shot a Masontown police officer in a lobby and then shot two men and a woman.

Another police officer from the German Township police department who ran into the building then shot the suspect several times, and the suspect died from his wounds, officials said.

The suspect was charged with strangulation, assault and other charges stemming from a domestic incident several weeks ago and he was due to have a hearing on Wednesday, Bower said.

Court was in session at the time of the shooting, but police do not believe that Judge Shimshock or any of his staff or prosecutors were targets of the shooting.

Multiple people were shot inside a magisterial district judge's office in Masontown, Pennsylvania. NBC Philadelphia

"I want to make it clear: Without the rapid response of these brave officers to the shooting, more individuals may have been shot," Pennsylvania State Police Lt. Steve Dowlin said at a news conference.

Wounded in the shooting was Masontown police Sgt. R. Scott Miller, he said. The officer was reported to be in good condition, Bower said.

One of the people who was at the judge’s office for a hearing was hiding in a bathroom with his mother after the shooting when a woman ran inside bleeding from the arm, the man’s father told NBC affiliate WPXI of Pittsburgh.

He used a tourniquet to help stop the bleeding and his feet to buttress the door during the ordeal, the man's father told the station.

Eric Randolph, an attorney who was in court when the shooting took place, said he heard a pop and then a "ruckus" that sounded like people being shoved against a wall and a door. He heard two pops, a "pow-pow" but at the time did not know they were gunshots.

He went to the courtroom door and "the door burst open, people started running in the room, I heard screaming — people yelling 'shooter! Shooter!,'" Randolph said. He took shelter against a wall near the bench where the judge hears cases.

Alan Brown, who was in the lobby of the magistrate's office, told WPXI that the gunman fired entered the building after a woman ran in and fired six to eight shots while he was inside. He told the station the gunman appeared to be following a woman running down a hallway before returning to the lobby where police had arrived.

"You could tell when police started firing," Brown told the station. "There was a lot more gunfire at that point."

The investigation is in its early stages, Dowlin said. A motive in the shooting, and whether anyone was targeted, was not disclosed.

The German Township police officer who killed the gunman, "protected over 30 to 40 people from injury or death," Bower said.

"It’s unfortunate that someone lost their life, but I’m proud of my officer," German Township Police Chief Dave Hromada said. "I think everyone did what they were trained to do."

Masontown is a borough of around 3,000 people about 40 miles south of Pittsburgh, according to Census data.