When authorities caught up to Yonathan Melaku, it was more than six months after a mysterious spree of overnight shootings at the Pentagon and other military buildings in Northern Virginia. Melaku was lurking in Arlington National Cemetery with a backpack filled with bags of ammonium nitrate and a can of black spray paint.

Federal prosecutors revealed Thursday that Melaku, a 23-year-old former Marine Corps Reservist, was on a mission to desecrate the graves of veterans who died in Iraq and Afghanistan. They said he was poised to spray paint Arabic statements on the markers and leave the explosive materials nearby, part of a solitary campaign of “fear and terror” that included the earlier shootings.

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 with prosecutors, Melaku admitted to using his legally-obtained 9mm handgun to shoot the National Museum of the Marine Corps, the Pentagon, and two other military buildings in October and November 2010.

The agreement calls for Melaku to serve 25 years in prison with five years of supervision after his release. Judge Gerald Bruce Lee accepted the plea, and Melaku is scheduled for sentencing in April.

Though Melaku acknowledged shooting at the buildings — attacks that did not injure anyone but caused an estimated $111,000 in damage, mostly to windows at the Marine Corps museum — 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 wants to target the museum and “turn it off permanently.”

FBI officials and prosecutors said Melaku was on a personal terror mission, but they could not say what started Melaku down that path. They said it does not appear his service in the Marine reserves provides any clear sign of trouble, but they said he researched jihadism on the Internet, had references to terrorism in a notebook and on his computer, and yelled “Allahu Akbar” repeatedly during a video of a shooting.

View Photo Gallery: Take a look back at the Yonathan Melaku case.

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, but authorities said it is unclear what might have been coming had he not been caught.

“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 on Thursday, answering the judge’s questions in low tones with “yes, sir” and “no, sir.” Two of his family members sat the back of the courtroom and declined to comment afterward.

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

The spree of shootings occurred in the late night and early morning hours. Initially they mystified local, state and federal law enforcement officials, who long believed that a disgruntled member of the armed forces likely was behind them.

Melaku was arrested inside Arlington National Cemetery on June 17 — seven months after the last known shooting — after he was spotted loitering at Fort Myer. Upon his capture, police said they found a small amount of bomb-making materials in Melaku’s backpack and discovered a video in his bedroom that showed him driving by the Marine Corps museum in Prince William County and firing repeatedly out the passenger window. Melaku appeared to be alone in the car.

“All right, next time this video turns on, I will be shooting," Melaku says on the video, court papers say. “That’s what they get. That’s my target. That’s the military building. It’s going to be attacked.”

Authorities later found instructions for making improvised explosive devices in Melaku’s home in the Alexandria section of Fairfax, and they found a notebook with references to Osama bin Laden and “The Path to Jihad.”

Melaku, a 2006 graduate of Edison High School in Franconia, is a naturalized citizen who moved to the United States from Ethi­o­pia in 2005, according to U.S. authorities. 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.

Before his arrest in the shootings, Melaku had been linked to a spate of vehicle break-ins in Loudoun County. He was found guilty of two counts of grand larceny in Loudoun in November and was also convicted of trying to escape from his jail cell by digging through a cinderblock wall. A plea agreement in that case ended with Melaku having to serve six months in jail.

This Post has been updated.

Staff writer Julie Tate contributed to this report.