A man with a history of mental illness and homelessness was ordered jailed on a first-degree murder charge Thursday in the stabbing of a 27-year-old D.C. woman who police say was attacked for no apparent reason while walking a dog in the Park View area of Northwest Washington.

The victim, Margery Magill, 27, who was a familiar sight in the neighborhood as a paid dog walker, was stabbed in the back, neck, shoulder and stomach by an assailant who confronted her about 8:45 p.m. Tuesday in the 400 block of Irving Street NW, according to D.C. police.

Shortly afterward, detectives followed a blood trail from the crime scene to an apartment about a quarter-mile away, in the 500 block of Columbia Road NW, according to an affidavit filed in D.C. Superior Court. There, Eliyas Aregahegne, 24, was watching television on a couch in his father’s residence, his left middle finger bleeding from a cut, the affidavit says.

Later, in a homicide interrogation room, Aregahegne “placed himself on scene and explained that a dark force was speaking to him from inside of his head,” a detective wrote in the affidavit. “At one point Defendant stated that when he went back outside the voices were speaking to him and things ‘Got out of hand.’ ”

Aregahegne told the detective, “I don’t remember stabbing her,” and, “I don’t think I stabbed her,” according to the affidavit.

At his initial appearance in Superior Court on Thursday, Aregahegne was ordered jailed pending the outcome of a Sept. 12 preliminary hearing. Police Chief Peter Newsham said detectives have ruled out robbery or sexual assault as a motive for the killing and have found no prior connection between the victim and suspect.

“This was a random, vicious, unprovoked attack on a woman who was stabbed multiple times and who was stabbed so viciously that her stomach separated from her aorta,” Magistrate Judge Rainey R. Brandt said from the bench Thursday, reviewing charging documents in the case. “The weight of the government’s evidence is strong.”

Atop a bathroom mirror in the apartment, police said, they found a white T-shirt stained with suspected blood. The T-shirt “appeared similar to the one worn by the suspect” as seen in a surveillance video from Irving Street, the affidavit says. It says a pair of Nike sneakers, also stained with suspected blood, was under the bathroom sink.

On the kitchen floor was “an empty opened ‘Armitage’ 8 inch knife package.”

Magill, who grew up in Yuba City, Calif., north of Sacramento, was a daughter of now-retired teachers. As a child, she raised goats and helped 4-H clubs with agricultural programs, her father, Jeffrey Magill, said. Since age 9 she was eager to see the world, and over the years she traveled to two dozen countries, he said.

A graduate of the University of California at Davis, she received a master’s degree in international relations from the University of Westminster in London and worked in the District as a project coordinator at the Washington Center, helping to place graduate students in jobs and internships around the globe.

To earn extra money, she walked dogs. When she was attacked Tuesday, she was walking a tiny yellow pit bull mix on Irving Street, about a mile from her apartment on 10th Street NW near Howard University.

Aregahegne’s adulthood has been marked by periods of homelessness and hospitalizations for mental illness.

Born in Ethi­o­pia, he moved to the United States with his parents when he was about 5 and attended the District’s Booker T. Washington Public Charter School, according to court documents in Montgomery County, Md., where he was arrested at least five times in recent years for alleged trespassing and other minor crimes.

On June 14, 2014, police were called to the 600 block of Irving Street NW — about two blocks from where Magill was later attacked — for a report of a “suicidal man,” according to records kept by the District’s then-Department of Mental Health, now called the Department of Behavioral Health.

Aregahegne, who was living with his mother at the time, was “actively hallucinating” and believed that the government was eavesdropping on him, according to the records, which are not publicly available but were obtained by The Washington Post.

He was gone from the Irving Street home for 48 hours before the June 14 incident, his mother told police. She said that after he returned, she heard him “mumbling” to himself that “Eliyas must die.” She said he then ran into the kitchen, intending to grab a knife, according to the records.

Two years later, in 2016, Aregahegne was admitted for observation at Washington Hospital Center because he showed “symptoms of mental illness” and appeared to be “a threat to himself and others,” according to the records.

Two months after that, the records say, he was detained by police and admitted to the Psychiatric Institute of Washington after trying to climb into the Tidal Basin “multiple times” and banging his head against a brick wall.

In Montgomery, four of his trespassing cases were dismissed, and one, involving several ­offenses, resulted in a guilty plea in May this year, court records show. He was on probation in that case when he allegedly attack Magill.

While some of the cases were pending early this year, Aregahegne was confined for several weeks in Maryland’s Springfield Hospital Center, a psychiatric facility.

“He initially reported . . . auditory hallucinations,” a mental-health professional wrote, adding that Aregahegne “complained of feeling as if energy were building up within him. . . . He endorsed feelings of grandiosity with a disorganized response about having super powers of ‘high science’ and how immortality can be revealed.”

Dan Morse contributed to this report.