Yonathan Melaku was sneaking through Fort Myer and Arlington National Cemetery, his backpack filled with plastic bags of ammonium nitrate, a notebook containing jihadist messages, and a can of black spray paint. The 23-year-old former Marine was heading to the graves of the nation’s most recent heroes, aiming to desecrate the stones with Arabic statements and leave handfuls of explosive material nearby as a message.

Before police foiled the plan in June, the vandalism was to be Melaku’s sixth attack, months after he went on a mysterious shooting spree that targeted the Pentagon, the National Museum of the Marine Corps and two other military buildings in Northern Virginia. A video found after Melaku’s arrest showed him wearing a black mask and shooting a 9mm handgun out of his Acura’s passenger window as he drove along Interstate 95, shouting “Allahu Akbar!”

It was all part of a solitary campaign of “fear and terror,” federal prosecutors said. But authorities and Melaku’s defense attorney said no one knows for sure what led Melaku — a naturalized U.S. citizen from Ethi­o­pia, local high school graduate and former Marine Corps Reservist — down that path or what message he was trying to send.

Melaku stood in U.S. District Court in Alexandria on Thursday morning to plead guilty to three counts, including shooting at the Pentagon on Oct. 19, 2010, and attempting to injure veterans’ memorials on U.S. property. As part of a plea agreement, Melaku admitted to using his legally obtained 9mm handgun to shoot the National Museum of the Marine Corps, the Pentagon and two military recruiting offices in October and November 2010.

The agreement calls for Melaku to serve 25 years in prison. Judge Gerald Bruce Lee accepted the plea, and Melaku is scheduled for sentencing on April 27.

Yonathan Melaku, 22, of Alexandria after his May 2011 arrest in Loudoun County, where he was charged with four counts of grand larceny. (The Associated Press)

Although Melaku acknowledged shooting at the buildings — attacks that did not injure anyone but caused an estimated $111,000 in damage — it still remains unclear why he did it. In a video entered into evidence and released by the U.S. Attorney’s Office for the Eastern District of Virginia, Melaku says that he was targeting the museum as a military building, to “turn it off permanently.”

FBI officials and prosecutors said Melaku was on a personal terror mission. They said he researched jihadism on the Internet and had references to terrorism in a notebook and on his computer. It also seemed like he was gathering materials to make an improvised explosive device, though there was no indication how he would have used it.

Melaku wanted “to create fear and terror, which is what terrorists do,” said Dana Boente, first assistant U.S. Attorney in Alexandria.

Special Agent Jacqueline Maguire, of the FBI’s Washington Field Office, said it was fortunate authorities caught Melaku before anyone was injured. She said it appears Melaku acted alone.

“This case shows that violent homegrown extremism is present in our community, whether by one person or by many,” Maguire said.

Melaku, of the Alexandria section of Fairfax County, did not make a statement during the hearing, answering the judge’s questions in low tones with “yes, sir” and “no, sir.” Two of his family members sat at the back of the courtroom and declined to comment afterward.

When asked what he wanted to plead to the three counts included in his agreement, Melaku leaned forward to a microphone and said: “Uh, guilty, sir.”

Gregory English, Melaku’s defense lawyer, said after the hearing that Melaku’s family is of the Coptic Christian faith and that they were stunned to learn of his involvement in the crimes and the references to Islamic jihad. English said the shootings were out of character for Melaku, and he wonders whether his client suffers from a psychological problem, which he has asked the court to evaluate.

English said Melaku thought that by shooting at the buildings he did, late at night, no one would get hurt. English said the video was intended for YouTube.

“As bad as it is, this is someone who essentially broke windows,” English said. “It’s vandalism. He has no link to terrorism. . . . He had a message, but I don’t understand what that message was supposed to be.”

Melaku took the plea agreement to avoid a possible mandatory sentence of more than 85 years in the face of solid evidence, English said. FBI agents tracked down Melaku’s Acura and the 9mm handgun — both of which Melaku sold after the shootings — and they found gunshot residue on the car and matched the bullets from the shootings to the gun.

Melaku was a 2006 graduate of Thomas Edison High School in Franconia, and English said he moved to the United States from Ethiopia as a youth. Marine Corps officials have said he joined the reserves in September 2007 and that he was never deployed overseas and was not scheduled to deploy.