Jarrod Ramos, accused of killing five people in a shooting rampage at the offices of the Capital Gazette newspaper in Annapolis, Md., admitted in court Monday that he committed the offenses, setting the stage for a jury to decide whether Ramos was so mentally ill at the time that he should not be held legally culpable.

Ramos, 39, who authorities said bore a long-simmering grudge against the paper, acknowledged in Anne Arundel County Circuit Court that he was the shotgun-wielding attacker who blasted through doors at the Capital Gazette on June 28, 2018, and methodically opened fire on employees.

Four journalists and a sales assistant were slain in what authorities described as an act of vengeance by Ramos, who had lost a defamation lawsuit against the paper after it published a column about his guilty plea to harassing a former high school classmate.

Judge Laura S. Ripken accepted Ramos’s admission after a hearing Monday afternoon. She scheduled the start of jury selection for Wednesday in the next phase of the case, in which Ramos’s attorneys will argue that he is not criminally responsible for the shooting and should be committed to a mental hospital instead of prison.

In Maryland, a defendant is entitled to an insanity acquittal if he proves that at the time of the offense, as a result of a mental disorder, he lacked “substantial capacity to appreciate” the criminality of his conduct or to conform his conduct to the requirements of the law.

If Ramos succeeded in that defense, he would be confined to a psychiatric institution for treatment and eventually would be eligible for release if it were decided that he was not likely to pose a danger to the public in the future.

18-year-old Sofia Biondi, likely the last person Wendi Winters interviewed, recounted her time with the journalist just before Winters was shot and killed. (Joyce Koh/The Washington Post)

The attack killed the Capital Gazette’s editorial page editor, Gerald Fisch­man, 61; assistant editor Rob Hiaasen, 59; sportswriter, reporter and editor John McNamara, 56; sales assistant Rebecca Smith, 34; and reporter Wendi Winters, 65. Six other employees in the office were not shot and escaped the attack.

In court Monday, as survivors of the shooting and loved ones of the dead looked on, Ramos appeared before the judge, clad in green jail clothing, with a flowing beard and his hair tied in a ponytail. He listened for an hour as State’s Attorney Anne Colt Leitess recounted previously undisclosed details of the attack.

Driving a rented Kia Rio, Ramos traveled from his home in Laurel, Md., to Annapolis, arriving at the newspaper shortly after 2:30 p.m. with a duffel bag containing smoke grenades, a shotgun, more than 50 rounds of ammunition and devices to barricade doors, Leitess said. She said he is seen on security video checking the gun’s flashlight and laser sight before trying to enter the office but encountering a locked door.

He opened fire, shattering the glass door.

“No! No! No! No!” a witness heard Smith yelling, Leitess said.

Reporters in the office ran for cover, hiding under desks while Winters grabbed a recycling bin and trash can to fend off the shooter. “You stop that!” she yelled at Ramos, according to Leitess, who was quoting a witness.

After the massacre, Leitess said, Ramos dialed 911 and told the call-taker: “This is your shooter. The shooting is over. I surrender.”

A greeting card, a CD that contained photos of Capital Gazette employees and surveillance video of the office was mailed to Eric Hartley, the former columnist who had written about Ramos’s harassment case, prosecutors said. Some of the names were labeled “High Value Targets,” Leitess said.

If Ramos had not admitted committing the crimes outlined in the 23-count indictment, Leitess said, survivors would have testified that they had to step over the bodies of slain co-workers as they evacuated the building after police arrived.

As officers searched the building, she said, Ramos was hiding beneath a desk. When they found him, he had discarded the safety glasses and orange earplugs he wore during the shooting. “I surrender, I surrender,” he said, according to Leitess. “I’m your shooter.”

In the pistol grip of his shotgun, Leitess said, police found a note with a quote from a book called “Active Shooter: Events and Response.” It said: “There are very few problems in the world that cannot be solved by clear and concise communication. The remaining problems can be solved with the proper placement and application of high explosives.”

Ramos’s admission that he carried out the attack spares the prosecution from having to prove to a jury that he was the shooter. With that first phase of the trial now unnecessary, the second phase will focus on his mental state at the time.

As Leitess on Monday described the rampage, survivors and relatives of the slain sat silently in the courtroom gallery, dabbing tears and rubbing one another’s shoulders.

“It’s hard to know how I feel at a time like this,” Andrea Chamblee, McNamara’s wife of 33 years, said after the hearing. “It’s a whipsaw of emotions, but there’s no name for it. I can’t really compare it to any other feelings I’ve ever experienced.”

Each new development in the case has brought a painful reminder: She has a Google alert set for her dead husband’s name. Every time an alert appears in her email, she said, “my stomach lurches. But I read them anyway.”

Andy Jezic, a Maryland criminal defense lawyer and former Anne Arundel County prosecutor, said he was not surprised that Ramos acknowledged being the gunman. “It sounds like there is overwhelming evidence that he is the killer, so there would be very little reason to have the jury hear all the horrific details about him shooting everybody. But I still think they have a right to a jury trial on the insanity or not-criminally-responsible stage,” said Jezic, who is not involved in the case.

Trials involving the insanity defense are exceedingly rare nationwide, and the success rate for defendants in those cases is minuscule. Usually when lawyers invoke the defense, they have valid reasons for doing so, and prosecutors typically end up conceding — before a case ever goes to trial — that the defendants aren’t legally guilty.

But because of the gruesome nature of the rampage and because Ramos’s mental state at the time of the shootings is unclear, Jezic said, it makes sense for prosecutors to leave the determination to a jury.

“When something is so heinous as this, the prosecutor needs the jury to make the final judgment,” Jezic said, noting that a jury decided that John W. Hinckley Jr. was not guilty by reason of insanity in the 1981 shooting of President Ronald Reagan and three others outside a Washington hotel.

Had Ramos gone to trial on whether he committed the offense, jurors could have been shown a video depicting the attack, under a ruling the judge made earlier after the defense team sought to block the footage from being shown.

Capital Gazette editor Rick Hutzell said Monday after the hearing that “you simply cannot silence a free press, which is what this guy wanted to do. We wrote about him fairly and accurately. He didn’t like it. When legal means were exhausted, he resorted to violence. Justice is for the living. There is no justice for the dead. Nothing that guy says will change that.”

Staff members at the newspaper said they had been preparing themselves emotionally for the trial for the past year, but Leitess’s description of the crime was still agonizing.

“It’s exhausting,” said Danielle Ohl, a reporter who was on vacation when the shooting occurred. “It’s a lot of emotion.”

Listening to Ramos’s 911 call was especially jarring. “I’d never heard him speak before,” Ohl said. “You think someone like that — someone who would do something like that — would be a monster. But he sounded like a normal guy.”

She said, “It almost makes it scarier because people who do this, they seem normal.”

Reis Thebault contributed to this report.