The centrist Liberals, led by Justin Trudeau, started the campaign in third place but in a stunning turnaround now command a majority.
Mr Trudeau, the 43-year-old son of late Prime Minister Pierre Trudeau, said Canadians had voted for real change.
Incumbent Conservative PM Stephen Harper – in power since 2006 – has congratulated his rival.
Justin Trudeau is son of late Prime Minister Pierre Trudeau, considered the father of modern Canada.
Trudeau’s to-do list
Addressing jubilant supporters, Mr Trudeau said Canadians had “sent a clear message tonight: It’s time for a change”.
“We beat fear with hope, we beat cynicism with hard work. Most of all we defeated the idea that Canadians should be satisfied with less.
“This is what positive politics can do,” he said, also praising Mr Harper for his service to the country.
During the 11-week election campaign, the Liberal Party said it would:
Cut income taxes for middle-class Canadians while increasing them for the wealthy
Run deficits for three years to pay for infrastructure spending
Do more to address environmental concerns over the controversial Keystone oil pipeline
Take in more Syrian refugees, and pull out of bombing raids against Islamic State while bolstering training for Iraqi forces
It was a sweeping victory that seemed unthinkable five years ago, improbable just months ago and unlikely even a few days ago.
And yet shortly after midnight on Tuesday, Justin Trudeau took the stage at Liberal headquarters as prime minister elect.
When Mr Harper first announced that this year’s general election campaign would be a record 78 days long, conventional wisdom was it would benefit the Conservatives, giving them more time to bring their financial advantages to bear.
In hindsight, however, the lengthy campaign gave Mr Trudeau an opportunity to introduce himself to Canadians and overcome Conservative attacks that characterised the 43-year-old former high-school drama teacher as too inexperienced to lead the Canadian nation.
Mr Trudeau also successfully outmanoeuvred the New Democratic Party, campaigning to that party’s left on economic issues.
For Mr Trudeau, the hard work of bringing the Liberal Party back from the ashes is over. Now, as the next prime minister of Canada, the even harder work of governing is about to begin.
Mr Harper, one of the longest-serving Western leaders, had been seeking a rare fourth term.
Speaking after the polls closed, he said he had congratulated Mr Trudeau, and that the Conservatives would accept the results “without hesitation”.
Mr Harper will stand down as Conservative leader but remain as an MP, his party says.
There is no fixed transition period under Canada’s constitution. Mr Trudeau is expected to be sworn in in a few weeks’ times.
Tom Mulcair of the left-leaning New Democratic Party (NDP) said he had “congratulated Mr Trudeau on his exceptional achievement”.
The NDP is on course to win 44 seats, less than half the number they held in the outgoing parliament.