CHARLESTON, S.C. — In early January, after a series of closed-court hearings, a federal judge in Charleston allowed Dylann S. Roof to represent himself as prosecutors sought his execution for a murderous 2015 rampage at a historic African-American church. Determined to block exposure of his mental health history, the 22-year-old white supremacist sidelined his expert defense team and presented no mitigating evidence. A jury took less than three hours to dispatch him to death row.

But the information that Mr. Roof worked so hard to suppress has now become public, as Judge Richard M. Gergel of Federal District Court this month finished unsealing thousands of pages of psychiatric reports and transcripts from the November and January hearings. The documents provide, for the first time, a multidimensional portrait of a withdrawn but strikingly intelligent misfit whose tastes ran to Dostoyevsky, classical music and NPR but who said his “dream job” would be working at an airport convenience store. He exhibited disturbingly introverted behavior from an early age — playing alone, never starting conversations — but received little treatment for what defense experts later concluded was autism and severe social anxiety, with precursor symptoms of psychosis.

Although Mr. Roof’s guilt was never in doubt, his trial left largely unanswered how an awkward adolescent had progressed from reclusive consumer of internet hate to ruthless and remorseless jihadist (his word). The evidence that went unseen affords a rare glimpse of the backstory of one of the country’s most horrific hate crimes. And it raises a life-or-death question: If the 12 jurors had been allowed a full accounting, might at least one have been persuaded to spare the young mass murderer from lethal injection?

Mr. Roof’s former lead defense lawyer, David I. Bruck, declined to comment. But Sean O’Brien, a law professor who advised him on mental health issues, questioned the trial’s ultimate fairness and predicted that Judge Gergel’s findings of competency would fuel the appeals that Mr. Roof hopes will extend his life. “A thorough defense would have enabled a jury to see not only who Dylann Roof is, but who Dylann Roof might have been in the absence of these afflictions,” said Mr. O’Brien, who teaches at the University of Missouri-Kansas City. “The jury didn’t have anything to weigh all of the pain and suffering against, or even to give it context.”

Despite its outcome, Mr. Roof’s trial went largely as he wished. The Justice Department had rejected his offer to plead guilty to 33 counts in exchange for a life sentence. He did not see much difference between a life term and execution. But it was imperative, he told his lawyers and psychiatric examiners, that his trial not distort or dilute his purpose for gunning down nine parishioners at Emanuel African Methodist Episcopal Church.

His strategy, he told Dr. James C. Ballenger, a forensic psychiatrist appointed by Judge Gergel, had been to mount the most outrageous assault imaginable in order to foment a race war.

“I don’t want anybody to think I did it because I have some kind of mental problem,” Mr. Roof, now 23, told the judge. “I wanted to increase racial tension.”

Document | Competency Hearing Transcript At a competency hearing, Dylann Roof sought to represent himself at trial.

Mr. Roof also said it was important that he be seen as “a perfect specimen,” unblemished by mental illness, reported Dr. Ballenger, a former chairman of psychiatry at the Medical University of South Carolina. Mr. Roof told another examiner that he might then be rescued after a white nationalist takeover, pardoned, and perhaps even installed as governor. With an autism diagnosis, he told Dr. Ballenger, “everybody would think I am a weirdo.”

The competency hearings, which were requested by Mr. Bruck, a noted death penalty antagonist, revealed a surreal conflict. As Mr. Roof’s court-appointed lawyers fought to save his life by constructing a mental health defense, he did anything he could to stymie them. The lawyers then argued that Mr. Roof’s rejection of his only route to survival was itself evidence of his incapacity.

In early November, Mr. Roof sabotaged them by sending a handwritten letter to the prosecution, knowing it could be introduced as evidence: “What my lawyers are planning to say in my defense is a lie.” Judge Gergel took seriously enough Mr. Roof’s occasional threats to stab Mr. Bruck that he warned the lawyers against placing pens within his reach.

Document | A Handwritten Letter In early November, Mr. Roof sent a handwritten letter to prosecutors.

All three experts retained by Mr. Bruck’s team — a forensic psychiatrist, a neuropsychologist and an autism specialist — concluded that Mr. Roof did not meet the legal standard for competency: the capacity to understand trial proceedings and assist in one’s defense. But Dr. Ballenger, the court-appointed psychiatrist, declared Mr. Roof competent, along with a consulting neuropsychologist.

The defense experts depicted a boy born into a strained marriage that ended when he was 5, whose home and school shifted with his mother’s relationships. Dr. Rachel Loftin, the defense expert who first diagnosed Mr. Roof’s autism while he was in jail, wrote that his condition might have been exacerbated by “a chaotic home environment, including exposure to domestic disputes and possible violence.” Mr. Roof performed well in elementary school, but began smoking marijuana at age 13 and soon graduated to prescription drugs and alcohol. His grades plummeted, and he dropped out in 10th grade.

Document | Psychological Evaluation A report by Rachel Loftin

Family members reported that for the next five years, Mr. Roof secluded himself in his room, often online, rarely communicating or leaving the house. He never dated. His older sister managed to lure him to a restaurant on his birthday only by dangling a $40 bribe. He showed little initiative to work or become independent. The one time he held a job, with a landscaping firm for two months, he ate lunch apart from co-workers and rarely spoke. He wore a hoodie “like a cocoon,” his mother’s boyfriend said.

Mr. Roof’s compulsive obsessions, according to the lawyers and examiners, ran from the brand of his acne wash to the fit and fabric of his clothing to a delusion that his forehead was misshapen (he obscured it with his trademark bowl haircut). He so feared being looked at that he begged his mother not to pull up alongside other cars.

The psychiatrists characterized the slightly built Mr. Roof as both childlike and grandiose, and noted his flat affect, incongruent jokes and dearth of empathy.

When his mother visited him in late December, her first trip to jail since suffering a heart attack on the trial’s opening day, he asked not about her health, but incessantly about why she had not purchased the pants he wanted to wear to court. At the close of another visit, Mr. Roof’s teenage half sister bade him farewell: “I love you, Dylann, even if you don’t love me back.”

“O.K.,” he answered.

Records show that Mr. Roof’s only contact with mental health providers came in 2009, when he made three visits to a clinic near Columbia, S.C., after threatening suicide to his mother. He reported persistent anxiety when around others and was given a prescription for an antidepressant, which he reportedly declined to take.

The documents include references to racist talk by Mr. Roof’s father, a house contractor, but there is no suggestion it was fundamental to Mr. Roof’s upbringing. Rather, the competency evaluators accepted his assertion that, spurred by the 2012 shooting of Trayvon Martin, he searched Google for “black-on-white crime,” came across hate-purveying websites and experienced a racial awakening.

“Before he did this research, he said he always had uncomfortable feelings that he could not explain,” wrote Dr. Donna Maddox, the defense team’s forensic psychiatrist. “Afterwards he felt suddenly better.”

Document | Psychological Evaluation Report by Donna Maddox

Absent alternate influences, wrote Dr. Loftin, clinical director of the autism center at Rush University Medical Center in Chicago, “Dylann pursued his preoccupation with racism with an autistic intensity.”

Dr. Ballenger, who was making his first competency determination, interviewed Mr. Roof for 13 hours and reached a contrary conclusion.

His report noted that Mr. Roof’s I.Q. of 125 placed him in the 95th percentile, and that he spoke coherently and logically. Once you understood Mr. Roof’s priorities, he wrote, his thinking about self-representation made a certain sense. Dr. Ballenger diagnosed several anxiety disorders, but not psychosis.

Judge Gergel, deeply concerned that the strongest mitigating evidence in a capital case would be quashed, initially warned Mr. Roof he would not let him represent himself.

“That is not, I believe, a proper defense in a situation like this, basically to let the government present its case and the jury then decide,” the judge said at the close of the first hearing.

Six days later, he had reversed course. He told the lawyers he had thought about Mr. Roof’s performance in the hearings, in which he had cross-examined Dr. Ballenger and persuaded him to alter a diagnosis. He had further studied the pertinent Supreme Court precedent, Indiana v. Edwards, which allows judges to require counsel for defendants who may be competent to stand trial but still suffer from such “severe mental illness” that they cannot conduct a defense.

“My thought about his wisdom, or lack of wisdom, in deciding to self-represent is not the issue,” Judge Gergel said. “I watched him for two hearings. He has the capacity to represent himself.”

The judge accepted Dr. Ballenger’s view that Mr. Roof’s strategy arose from political zealotry, not mental illness, and dismissed Mr. Bruck’s warnings that his intelligence masked deeply delusional thinking. “If this defendant were incompetent to represent himself,” he wrote, “almost no defendant would be competent to represent himself.”

Document | Judge Gergel’s Opinion Ruling on the question of self-representation.

For Mr. Roof, the decision meant that the jury would learn almost nothing about him beyond the brutal heartlessness of his crime. In April, he was moved to the United States Penitentiary in Terre Haute, Ind., site of the federal death row. Judge Gergel has appointed new lawyers for his appeals.