After a tight campaign that saw the two leading parties struggle to break out of the pack, the Liberals under Justin Trudeau have held on to just enough seats in Atlantic Canada, Quebec and Ontario to secure a minority government.

While final ballots are still being counted in several ridings, the Liberals are expected to win 157 seats, 20 fewer than they started with 40 days ago. The Conservatives moved up from 95 seats to an estimated 121, while the NDP lost 15 seats to end up with 24.

The Liberals managed to win despite preliminary numbers showing the Conservatives won the popular vote, taking 34.5 per cent of the vote to the Liberals' 33 per cent. That result was most pronounced in the Prairies, where Conservative turnout and support was very high.

In Alberta, the Conservatives won almost 70 per cent of the popular vote to the Liberals' almost 14 per cent. In Saskatchewan, the Tories took 65 per cent of the vote to the Liberals' 10 per cent.

Liberal Party supporters react as they watch the live federal election results at the Palais des Congres in Montreal, Quebec. (Stephane Mahe/Reuters)

The Liberals managed to hang on to power by holding their ground in both Quebec and Ontario. At dissolution the Liberals had 76 seats in Ontario; they are now poised to win 77 there. In Quebec, the Liberals went into the election with 40 seats and — despite the surging fortunes of the Bloc Québécois — are set to lose only five of those.

The Liberals took 26 of 32 seats in Atlantic Canada — a region the party swept in 2015.

Trudeau thanked his supporters, his campaign team, staff and candidates for their hard work. "You did it, my friends. Congratulations," he said.

"From coast to coast to coast tonight, Canadians rejected division and negativity. They rejected cuts and austerity and they voted in favour of a progressive agenda and strong action on climate change.

"It has been the greatest honour to serve you for these past four years and tonight you're sending us back to work for you. We take this responsibility seriously and we will work hard for you, for your families and for your future."

After failing to win a single seat in Saskatchewan or Alberta, Liberal Leader Justin Trudeau says he has heard those provinces' frustrations. 2:01

In his concession speech, Conservative Leader Andrew Scheer thanked his supporters and volunteers for their hard work during the campaign, telling them that the Conservatives are "on the march."

"The strength of our democracy is measured not only by the ballots we cast but also how we move forward after they're counted," Scheer said.

"While's tonight's result isn't what we wanted, I am also incredibly proud — proud of our team, proud of our campaign and proud of the bigger and stronger Conservative team that we will send to Ottawa."

"Tonight Conservatives have put Justin Trudeau on notice and Mr Trudeau, when your government falls Conservatives will be ready and we will win."

Conservative Leader Andrew Scheer says he's looking forward to heading back to Ottawa with a bigger Conservative team while speaking to reporters in Regina, Sask. 1:29

It was an evening of surprises — among them, Liberal Adam van Koeverden's victory over Conservative Deputy Leader Lisa Raitt in the Ontario riding of Milton, and the election of the first Green Party MP outside of B.C. — Jenica Atwin in Fredericton, N.B. — to bring the Green caucus in the House of Commons up to three.

"I served Miltonians for 11 years. There's a lot of people who never make it into the House of Commons and I've been so privileged to be there," Raitt told CBC News after her loss.

"It's not the win that we wanted to have this evening but I'm among my friends, and I'm among my family and everybody who's been with me for 11 years, so we're going to turn it into a celebration of what we've been able to accomplish."

In her concession speech, Ontario Conservative candidate Lisa Raitt says it has been an honour to serve as the deputy leader of her party. 0:51

Goodale goes down to defeat

Long-time Liberal MP and cabinet minister Ralph Goodale lost his Regina-Wascana to Conservative challenger Michael Kram. During his concession speech, Goodale thanked his volunteers and constituents for mandates in the past.

He also expressed gratitude for the many close personal friendships he's made over his years in politics.

"I will always treasure very, very deeply those personal friendships and the bonds that have tied us together," he said. "Because they have been built out of mutual respect and trust, commitment to principles and ideals ... never sparing an ounce of effort, always being willing, all of us together, to go the extra mile to try very hard to accomplish that little bit more for the people that we had the duty and obligation to represent in this riding."

"I've been a loyal Liberal in Saskatchewan for a very long time, much longer than a lot of people would have predicted," Liberal candidate Ralph Goodale says after losing in the federal election. He spoke with reporters his Regina-Wascana riding. 0:53

The Liberals took losses across the country. In Alberta, the party lost all three of its seats. Kent Hehr, who has served as the minister of Veterans Affairs and the associate minister of National Defence, lost his seat in Calgary-Centre to Conservative Greg McLean.

Hehr faced allegations of sexual harassment and stepped away from his cabinet posts but was allowed to remain a member of the Liberal caucus.

Amarjeet Sohi, the Liberals' former natural resources minister, was defeated in Edmonton Mill Woods by Conservative Tim Uppal.

The Liberals also lost the Alberta riding of Calgary Skyview — a seat they won in 2015 with Darshan Kang, who subsequently left the party over sexual harassment allegations — and Edmonton Centre.

Former Liberal justice minister and attorney general Jody Wilson-Raybould, who left cabinet and then was expelled from the Liberal caucus during the SNC-Lavalin affair, won as an Independent in her B.C. riding of Vancouver Granville.

PPC leader Bernier loses seat

People's Party of Canada Leader Maxime Bernier lost his seat in the riding of Beauce, Quebec to Conservative candidate Richard Lehoux. In his concession speech, Bernier thanked the 315 candidates across the country who made "huge personal sacrifices" to run for his fledgling party.

"They showed extraordinary courage and passion in defending our principles and policies. They did it despite nasty and shameless attacks from our opponents," he said.

Despite the nationwide defeat, Bernier vowed to keep the party alive to fight in the next election.

"There is no other option for our country. We will be stronger the next time. We will continue to fight for freedom, responsibility, fairness and respect. It's only the beginning for the People's Party."

After his party failed to gain a seat, People's Party of Canada Leader Maxime Bernier says the issues his party raised aren't going away. 2:18

Bloc will 'go even further': Blanchet

The Bloc Québécois, which went into the election with only 10 seats, has won 29 seats in Quebec and is currently leading in another three, signaling another dramatic change to the federal political landscape in that province.

"We've come a long way but we will go even further," leader Yves-François Blanchet told his supporters. "You welcomed me with such warmth it went well beyond my expectations."

The NDP, which started the federal election with 14 MPs in Quebec, appears to have lost most of those seats to the Bloc, with Alexandre Boulerice being the exception in Rosemont-La-Petite-Patrie.

NDP Leader Jagmeet Singh says his party will fight for their priorities in Ottawa. 1:55

NDP Leader Jagmeet Singh said he's proud of his party's focus on proposing solutions to help people who are struggling, and said his elected MPs will have a constructive, positive role in the new Parliament.

"When we get back to Ottawa, every single day that we're in Parliament, New Democrats are going to be working hard to make sure your life is better, that Canadians' life is better, that peoples' lives are better," he said.

Trudeau won his Montreal riding of Papineau. Scheer won in Regina-Qu'Appelle. Green Party Leader Elizabeth May won her riding of Saanich-Gulf Islands and Singh won his seat in the Vancouver riding of Burnaby South.

May told her supporters that despite not doing as well as Green supporters would have liked, she would use her party's three seats to push for climate action in Ottawa. "We can make a really significant contribution in a minority Parliament and we will," she said.

"I think and I know in my heart that all Canadians do care about the future, they do care about the climate crisis," said May."

"We can make a really significant contribution in a minority parliament," Green Party Leader Elizabeth May says to supporters in Victoria, B.C. Greens picked up 3 seats in the 2019 federal election. 1:13

Elections Canada said roughly 27.1 million people were eligible to vote at one of the approximately 20,000 polling places across the country.

There were 55,515 Canadian expats registered to vote, and a final tally shows 31,798 — a record number — have marked ballots.