One gunman was killed, and police said they were hunting for two others.
The bloodshed immediately raised the spectre of a co-ordinated terrorist attack, with Canada already on alert because of a deadly hit-and-run earlier in the week against two Canadian soldiers by a man who police say was fired up with radical Muslim fervour.
Witnesses said the soldier, who is being named by Canadian media as Nathan Cirillo, 24, was gunned down at point-blank range by a man carrying a rifle and dressed all in black, with a scarf over his face. They said the gunman ran off and entered Parliament, a few hundred yards away, where dozens of shots rang out.
People fled the complex by scrambling down scaffolding erected for renovations, while others took cover inside as police with rifles and body armour took up positions outside and cordoned off the normally bustling streets around parliament.
Police gave no details on how the gunman died. But on Twitter, MP Craig Scott credited parliament sergeant-at-arms Kevin Vickers with shooting the attacker just outside the MPs’ caucus rooms.
Ottawa police spokesman Chuck Benoit said two or three gunmen were believed to be involved in the attacks. Gilles Michaud, assistant commissioner of the Royal Canadian Mounted Police, called it a “dynamic, unfolding situation”.
Ottawa Hospital said it had received two patients, both said to be in a stable condition, in addition to the soldier.
Tony Zobl, 35, said he witnessed the soldier being gunned down from his fourth-floor window directly above the National War Memorial, a 70ft (21m), arched granite cenotaph, or tomb, with bronze sculptures commemorating the First World War.
“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 honour guard in front of the cenotaph point-blank, twice,” Mr Zobl told the Canadian Press news agency.
“The honour guard dropped to the ground, and the shooter kind of raised his arms in triumph, holding the rifle.”
Mr Zobl said the gunman then ran up the street towards Parliament Hill.
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 MP tweeted.
A spokesman for prime minister Stephen Harper said he was safe and had left Parliament Hill. The US Embassy in Ottawa was locked down as a precaution.
Officials also cancelled two events in Toronto honouring Pakistani teenager and Nobel laureate Malala Yousafzai, including one in which she was supposed to receive honorary Canadian citizenship. The teenager was shot in the head by a Taliban gunman in 2012 for calling for schooling for girls.
US President Barack Obama condemned the attacks as “outrageous” and spoke by telephone with Mr Harper, offering to help and reassuring him of the American people’s solidarity with Canada.
The attack came two days after a recent convert to Islam killed one Canadian soldier and injured another with his car before being shot dead 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 yesterday because of “an increase in general chatter from radical Islamist organisations”, said Jean-Christophe de Le Rue, a spokesman for the public safety minister.
In the hours after yesterday’s attack, police warned people in central Ottawa to stay away from windows and rooftops.