ANNAPOLIS, Md. -- A Maryland man pleaded guilty Monday in the slayings of five employees at an Annapolis newspaper, and a jury is expected to hear testimony next month about whether he was not responsible by reason of insanity.

Jarrod Ramos pleaded guilty in the 2018 massacre at the Capital Gazette newspaper. Judge Laura Ripken accepted his plea Monday afternoon.

Ripken said she determined that Ramos "freely, knowingly and voluntarily" waived his right to a jury trial to determine his guilt or innocence.

Wearing green jail clothes and a long beard, Ramos stood in court and repeatedly answered "that is correct" when asked by the judge whether he understood he was giving up his right to the first phase of his trial to determine whether he committed the crimes.

"Yes, I am," Ramos said, when asked if he was guilty of all 23 courts in an indictment, including first-degree murder, attempted murder, assault and other charges.

Newspaper employees and victims' relatives who attended the hearing held hands and wiped at tears during the proceeding in Anne Arundel County Circuit Court.

Rachael Pacella, a Capital Gazette staffer who survived the shooting by hiding between file cabinets and was injured while trying to escape, said Ramos' guilty plea brought a "big emotional relief" for her.

"I definitely feel a little bit better and a little bit lighter after this plea," she said.

Rick Hutzell, the newspaper's editor, said he hopes Ramos' guilty plea provides some comfort to the survivors of the shootings and relatives of the five employees who were killed. But he said he doesn't know if it brings any justice.

"There is no justice for the dead," Hutzell said.

Phil Davis, a former Capital Gazette reporter who survived the shooting and now works as a reporter at The Baltimore Sun, told reporters outside the courthouse while the case isn't over, "there's a certain level of closure that this brings."

"Having to not go through this part of the trial, which would have been an argument of his guilt, certainly seems validating," Davis said.

The 39-year-old Ramos had originally pleaded not guilty and not criminally responsible. The latter term is Maryland's version of an insanity defence.

The judge said at a pretrial hearing last week that a report from the state health department has found Ramos to be legally sane. Attorneys for Ramos have said experts on the defence team reached a different conclusion.

Under Maryland law, a defendant has the burden to show by a preponderance of the evidence that he is not criminally responsible for his actions. State law says a defendant is not criminally responsible for criminal conduct if, because of a mental disorder or developmental disabilities, he lacked substantial capacity to appreciate the criminality of his conduct.

If Ramos were found not criminally responsible, he would be committed to a maximum-security psychiatric hospital.

The judge had granted a request from defence attorneys to hold the trial in two phases -- the first to determine whether he committed the crimes and the second to decide whether he wasn't criminally responsible. The guilty pleas Monday means the case will go straight to the second phase.

According to the state's attorney, jury selection is expected to start Wednesday. The proceeding to determine whether Ramos is criminally responsible will begin in November.

Authorities say Ramos stormed the newspaper office in June 2018 with a shotgun and killed John McNamara, Wendi Winters, Rebecca Smith, Gerald Fischman and Rob Hiaasen. It was one of the deadliest attacks on journalists in U.S. history.

Much of the rampage was captured on video surveillance, which the judge ruled earlier this month could be used at trial. The video shows Ramos testing a flashlight and laser sighting on a shotgun before he blasted his way through a locked door to begin the attack, said Anne Colt Leitess, the state's attorney prosecuting the case.

Leitess recounted in a statement of facts how Ramos had sealed a back door to prevent people from escaping.

Survivors had to step over the body of Winters as authorities led them out, Leitess said. Winters had charged at Ramos with a trash can and recycling bin during the attack, Leitess added.

Before the shooting, Ramos had been accused of harassing the Capital Gazette's staff in connection with a defamation suit he filed against the newspaper in 2012, authorities have said. The suit was thrown out.

Police said Ramos was angered that the newspaper reported on his guilty plea to a misdemeanour harassment charge about a decade ago after a former high school classmate alleged he was stalking and harassing her.

Police say they arrested Ramos after the shooting as he hid under a desk in the newsroom.

Leitess described how Ramos gave himself up to police.

"I surrender. I surrender. I'm your shooter," Ramos said, according to Leitess' statement of facts.

------

Associated Press writer Denise Lavoie contributed to this report from Richmond, Va.