"We will not be intimidated. Canada will never be intimidated," Prime Minister Stephen Harper vowed in an address to the nation.
Unfolding just before 10 a.m., while lawmakers were meeting in caucus rooms, the assault rocked Parliament over and over with the boom of gunfire, led MPs to barricade doors with chairs and sent people streaming from the building in fear. Harper was addressing a caucus when the attack began outside the door, but he safely escaped.
Investigators offered little information about the gunman, identified as 32-year-old ex-convict Michael Zehaf-Bibeau. But Harper said: "In the days to come we will learn about the terrorist and any accomplices he may have had."
Canada was already on alert at the time because of a deadly hit-and-run assault Monday against two Canadian soldiers by a man Harper described as an "ISIL-inspired terrorist."
ISIL, or Islamic State, has called for reprisals against Canada and other Western countries that have joined the U.S.-led air campaign against the extremist group in Iraq and Syria.
Witnesses said the soldier posted at the National War Memorial, identified as Cpl. Nathan Cirillo, was gunned down at point-blank range by a man carrying a rifle and dressed all in black, his face half-covered with a scarf.
The gunman appeared to raise his arms in triumph, then entered Parliament, a few hundred yards away, where dozens of shots soon rang out, according to witnesses.
People fled the complex by scrambling down scaffolding erected for renovations, while others took cover inside as police with rifles and body armor took up positions outside and cordoned off the normally bustling streets around Parliament.
On Twitter, Canada's justice minister and other government officials credited 58-year-old sergeant-at-arms Kevin Vickers with shooting the attacker just outside the MPs' caucus rooms. Vickers serves a largely ceremonial role at the House of Commons, carrying a scepter and wearing rich green robes, white gloves and a tall imperial hat.
At least three people were treated for minor injuries.
In Washington, President Barack Obama condemned the shootings as "outrageous" and said: "We have to remain vigilant." The U.S. Embassy in Ottawa was locked down as a precaution, and security was tightened at the Tomb of the Unknown Soldier at Arlington National Cemetery just outside Washington.
Harper vowed that the attacks will "lead us to strengthen our resolve and redouble our efforts" to keep the country safe and work with Canada's allies to fight terrorists.
Police said in the initial hours that as many as two other gunmen may have taken part in the attacks. But by late in the evening, the cordon around Parliament was lifted and police said there was no longer any threat to the public in the area.
Court records show that a man with the same name and year of birth as the gunman was charged with robbery in Vancouver in 2011. He pleaded guilty to the lesser charge of uttering threats and was sentenced to a day in jail plus credit for the 66 days he had already served.
Tony Zobl said he witnessed the Canadian soldier being gunned down from his fourth-floor window directly above the National War Memorial, a 70-foot, arched granite cenotaph, or tomb, with bronze sculptures commemorating World War I.
"I looked out the window and saw a shooter, a man dressed all in black with a kerchief over his nose and mouth and something over his head as well, holding a rifle and shooting an honor guard in front of the cenotaph point-blank, twice," Zobl told the Canadian Press news agency. "The honor guard dropped to the ground, and the shooter kind of raised his arms in triumph holding the rifle."
The Canadian Broadcasting Corp. had video of the gunman going to his car alone with his weapon after the shooting at the memorial. The car was later spotted parked in front of Parliament Hill, just down the block.
Cabinet minister Tony Clement tweeted that at least 30 shots were heard inside Parliament, where Conservative and Liberal MPs were holding their weekly caucus meetings.
"I'm safe locked in a office awaiting security," Kyle Seeback, another member of Parliament, tweeted.
"I was just taking off my jacket to go into caucus. I hear this pop, pop, pop. Possibly 10 shots, don't really know. Thought it was dynamite or construction rather than anything else," said John McKay, a member of Parliament.
He said security guards then came rushing down the halls, herding them toward the back of the buildings.
"And then we started talking to another woman and she was apparently inside the library of Parliament, saw the fellow, wearing a hoodie, carrying a gun," McKay said, "and then the implications of this start to sink in."
The attack came two days after a recent convert to Islam killed one Canadian soldier and injured another with his car before being shot to death by police. The killer had been on the radar of federal investigators, who feared he had jihadist ambitions and seized his passport when he tried to travel to Turkey.
Canada had raised its domestic terror threat level from low to medium Tuesday because of what it called "an increase in general chatter from radical Islamist organizations." As recently as Tuesday, Canada sent eight fighter jets to the Mideast to join the battle against Islamic State.
After the shootings, officials canceled two events in Toronto honoring Pakistani teenager and Nobel laureate Malala Yousafzai, including one in which she was supposed to receive honorary Canadian citizenship. She was shot in the head by a Taliban gunman in 2012 for supporting schooling for girls.