Navy Yard gunman Aaron Alexis had sought treatment for insomnia in the emergency rooms of two Veterans Affairs hospitals in the past month, but he told doctors he was not depressed and was not thinking of harming others, federal officials said Wednesday.

Those walk-in visits came just two weeks after Alexis had called police in Rhode Island to report hearing voices and feeling vibrations sent through his hotel-room walls. On Aug. 23, he went to a VA hospital in Providence. Five days later, he went to another one in Washington, seeking a refill of the medication he had been prescribed in Rhode Island, according to the officials, who spoke on the condition of anonymity because the investigation is ongoing.

In both cases, doctors sent Alexis home with the medication, identified by law enforcement officials as Trazodone, a generic antidepressant that is widely prescribed for insomnia. The VA doctors told him to follow up with a primary-care doctor. It is unclear whether he did.

“Mr. Alexis was alert and oriented, and was asked by VA doctors if he was struggling with anxiety or depression, or had thoughts about harming himself or others, which he denied,” the Department of Veterans Affairs said in a memo sent to Congress on Wednesday.

That report adds to a grim and frustrating portrait of Alexis, who killed 12 people at the Washington Navy Yard on Monday morning, wielding a shotgun with bizarre messages carved into the stock. One said “Better off this way” and another said “My ELF weapon,” law enforcement officials said.

Read eyewitness accounts from the Navy Yard shooting.

This was a man who often did not hide his problems. Alexis had left records of his troubles in local police reports, in Navy files and in VA medical records. But it was never quite enough to set off broader alarms or to revoke any of the privileges that the government had extended Alexis as an IT contractor.

In fact, this year — as Alexis’s mental state seemed to decline — government agencies affirmatively signed off on all the tools of his massacre.

A security clearance. An ID badge. Then, a gun.

“Obviously there were a lot of red flags,” Defense Secretary Chuck Hagel said Wednesday, saying there were valid questions about “why they didn’t get picked up.”

“Where there are gaps, we will close them,” Hagel said. “Where there are failures, we will correct them.”

In New York on Wednesday, Alexis’s mother made her first public comment since the mass shooting. “I don’t know why he did what he did, and I’ll never be able to ask him why,” Cathleen Alexis said, reading a statement to a pool reporter from her brownstone apartment in Brooklyn. “Aaron is in a place where he can no longer do harm to anyone. For that I am glad.”

A pastor who was with her told Alexis that she was a victim as well. But she played down her own loss, saying that the tragedy was not about that but about the victims.

“To the families of the victims, I am so very sorry that this happened,” Alexis said. “My heart is broken.”

In Washington on Wednesday, hospital officials said three of the injured victims had improved. D.C. police officer Scott Williams, who was shot in the leg, and an unidentified woman shot in the shoulder were upgraded from “fair” to “good” condition. They said a woman with gunshot wounds to her head and hand was discharged Tuesday.

At the Navy Yard on Wednesday, a stream of workers came back to retrieve cars or go to their jobs as personnel in camouflage patrolled the sidewalk with assault rifles. On Thursday, officials said, the Navy Yard would return to “near normal operations,” with most workers expected back in their offices.

But Building 197, the scene of the rampage, will remain closed. It remains an active crime scene — and a reminder that “near normal” at the Navy Yard is an elusive goal.

In the broader investigation, authorities were trying make sense of the phrases Aaron Alexis had carved into the stock of the Remington 870 pump-action shotgun he used in the rampage.

The reference to “ELF” was particularly baffling. That acronym can mean “extremely low frequency” and can refer to weather or communications efforts, among other things.

Alexis told police in Rhode Island in August that he was hearing voices of three people who had been sent to follow him and keep him awake and were using “some sort of microwave machine” to send vibrations into his body, preventing him from falling asleep, according to police reports. Law enforcement officials said they do not know whether he was referring to those vibrations in his carvings.

In addition, the details of Alexis’s visits to VA hospitals provide a view into his efforts to seek help.

Alexis, a Navy veteran, was already enrolled in the VA health-care system. Shortly after he left the service in 2011, he filed a disability compensation claim with VA. In December 2011, he was granted a 20 percent disability rating for “orthopedic issues,” according to VA.

A year later, the rating was increased to 30 percent. An additional 10 percent was awarded for tinnitus, a condition that involves ringing in the ear. Based on those disabilities, Alexis was receiving $395 a month, according to VA.

In August, Alexis walked into the Providence hospital seeking treatment for another problem, about which he had long complained to friends: sleeplessness.

The drug Alexis was prescribed, Trazodone, is often used to treat depression. But it is also prescribed for sleeplessness and schizophrenia, according to the National Institutes of Health. It increases the amount of serotonin in the brain, a chemical that helps maintain mental balance.

Also Wednesday, the gun store that sold Alexis the shotgun released new details about the day he bought it.

Alexis came to Sharpshooters Small Arms Range in Fairfax County on Saturday and tested out an AR-15 semiautomatic rifle. While in the store, Alexis also inquired about buying a handgun, according to the store’s attorney.

But he didn’t make that purchase. In the long lead-up to Alexis’s spree, this was one time when he was denied something he wanted.

The reason was not Alexis’s arrests or his struggles with mental health. It was his out-of-state address. Federal law does not allow dealers to sell handguns directly to out-of-state residents, the gun shop’s attorney said. The gun would have had to be shipped to a licensed dealer in Alexis’s home state.

Instead, he bought the shotgun, which Virginia law allows out-of-state residents to purchase on the spot. Alexis passed federal and state background checks and took the weapon home, along with two boxes of shells.

Peter Hermann, Tom Jackman, Annys Shin, Lena H. Sun, Rachel Weiner, Alice Crites, Jennifer Jenkins and Julie Tate contributed to this report.