Canadian Prime Minister Mark Carney's Liberal party has staged a major political comeback to retain power in parliamentary elections.
The vote was fuelled by a backlash against US President Donald Trump's tariffs and comments on making Canada the 51st US state.
With almost all of the votes counted, the Canadian Broadcasting Corporation forecast the Liberals would form a strong minority government, falling just short of the 172 electoral districts needed for a majority.
Provisional results from Elections Canada showed the Liberals had won 169 districts, followed by the Conservative party with 144.
Judicial recounts will be needed in two districts and a final official result may be days away.
Although he failed to get a majority, Mr Carney faces no immediate political danger, since all opposition legislators would have to vote against him to topple the government and two of his main rivals are in political trouble.
Jagmeet Singh announced he would step down as leader of the left-leaning New Democrats after he failed to be re-elected in British Columbia and saw most of his party's MPs defeated.
Watch: System of open global trade anchored by US is over - Carney
Mr Carney had sought a majority to help him negotiate with President Trump on the tariffs threatening Canada's economy.
Mr Trump congratulated Mr Carney on his election victory in a phone call, the prime minister's office said in a statement.
"The leaders agreed on the importance of Canada and the United States working together - as independent, sovereign nations - for their mutual betterment. To that end, the leaders agreed to meet in person in the near future," it added.
The Globe and Mail newspaper, citing a senior Liberal official, said that Mr Carney would name a cabinet and reconvene the House of Commons within two weeks.
Another priority will be the annual budget, which is usually presented in March or April.
Mr Carney, saying President Trump wanted to break Canada, had repeatedly vowed to open talks on new security and economic ties with the US leader as soon as possible after the election.
The Liberals, who have been in power since November 2015, were 20% behind in surveys in January before the unpopular Justin Trudeau announced he was quitting as prime minister and Mr Trump started threatening tariffs and annexation.
"It was the 'anybody-but-Conservative' factor, it was the Trump tariff factor, and then it was the Trudeau departure ... which enabled a lot of left-of-centre voters and traditional Liberal voters to come back to the party," President of the Angus Reid Institute polling firm Shachi Kurl said.

In his victory speech, Mr Carney said that Canada's old relationship with the US was over.
"The system of open global trade anchored by the United States ... (which) has helped deliver prosperity for our country for decades, is over. These are tragedies, but it's also our new reality," he said.
Mr Carney said the coming months would be challenging and require sacrifices.
He also has to deal with Danielle Smith, the conservative premier of the western oil-producing province of Alberta, who complains the Liberals have undermined national unity with climate policies she says are designed to hurt the energy industry.
"A large majority of Albertans are deeply frustrated that the same government that overtly attacked our provincial economy almost unabated for the past 10 years has been returned to government," she said in a statement.
Mr Poilievre conceded defeat to the Liberals and said his party would continue to hold the government to account.
Mr Carney had promised a tough approach to Washington's import tariffs and said that Canada would need to spend billions of dollars to reduce its reliance on the US.
But the right-of-centre Conservatives showed unexpected strength.
Watch: We will do our job of holding the government to account - Poilievre
Minority governments in Canada rarely last longer than two-and-a-half years.
President Trump's threats spurred a wave of patriotism that boosted support for Mr Carney, a political newcomer who had previously led the Canadian and British central banks.
Mr Trump re-emerged as a campaign factor last week, declaring that he might raise a 25% tariff on Canadian-made cars because the US did not want them.
He had also said that he might use "economic force" to make Canada the 51st state.
Mr Carney argued that his experience handling economic issues makes him the best leader to deal with the US president while Mr Poilievre tapped into concerns about the cost of living, crime and a housing crisis.