In addition to those killed, eight people were hurt — three of them shot and wounded, according to the mayor. Those wounded were a police officer and two female civilians, authorities said. They were all expected to survive.
The FBI would not give any details on the gunman's weaponry, but witnesses said the man they saw had a long gun — which can mean a rifle or a shotgun.
The Washington Navy Yard is a sprawling, 41-acre labyrinth of buildings and streets protected by armed guards and metal detectors, and employees have to show their IDs at doors and gates. More than 18,000 people work there.
The rampage took place at Building 197, the headquarters for Naval Sea Systems Command, which buys, builds and maintains ships and submarines. About 3,000 people work at headquarters, many of them civilians.
Todd Brundidge, an executive assistant with Navy Sea Systems Command, said he and co-workers encountered a gunman in a long hallway on the third floor. The gunman was wearing all blue, he said.
"He just turned and started firing," Brundidge said.
Terrie Durham, an executive assistant with the same agency, said the gunman fired toward her and Brundidge.
"He aimed high and missed," she said. "He said nothing. As soon as I realized he was shooting, we just said, 'Get out of the building.' ""
As emergency vehicles and law enforcement officers flooded streets around the complex, a helicopter hovered, nearby schools were locked down and airplanes at nearby Reagan National Airport were grounded so they would not interfere with law-enforcement choppers.
Security was tightened at other federal buildings. Senate officials shut down their side of the Capitol while authorities searched for the potential second attacker. The House remained open.
In the confusion, police said around midday that they were searching for two men who may have taken part in the attack - one carrying a handgun and wearing a tan Navy-style uniform and a beret, the other armed with a long gun and wearing an olive-green uniform. Police said it was unclear if the men were members of the military.
But as the day wore, police dropped one person and then the other as suspects.
As tensions eased, Navy Yard employees were gradually being released from the complex, and children were let out of their locked-down schools.
Adm. Jonathan Greenert, chief of naval operations, was at the base at the time the shooting began but was moved unharmed to a nearby military installation.
Anxious relatives and friends of those who work at the complex waited to hear from loved ones.
Tech Sgt. David Reyes, who works at Andrews Air Force Base, said he was waiting to pick up his wife, Dina, who was under lockdown in a building next to where the shooting happened. She sent him a text message.
"They are under lockdown because they just don't know," Reyes said. "They have to check every building in there, and they have to check every room and just, of course, a lot of rooms and a lot of buildings."