Alexander Van der Bellen has won Austria's presidential election, preventing Norbert Hofer from becoming the EU's first far-right head of state.
Mr Van der Bellen, a pro-EU independent backed by the Greens, beat Mr Hofer by just 31,000 votes among the 4.64m cast.
The president-elect vowed to address the "divisions" among Austrians that the poll had "made visible".
Mr Hofer's campaign had targeted anti-EU feelings and fears about migrants. He said his defeat was a "sad day".
The Freedom Party candidate said on his Facebook page (in German): "Please don't be disheartened. The effort in this election campaign is not wasted, but is an investment for the future."
The interior ministry said Mr Van der Bellen had won 2,254,484 votes to Mr Hofer's 2,223,458, or 50.3% to 49.7%.
Although Mr Van der Bellen, 72, is officially independent, he led Austria's Greens for a decade and some European Green politicians were hailing him as the world's first elected Green head of state.
Speaking after his victory, he said he accepted many Austrians had different views and that some people were angry, but he added: "People can be different and still treat each other respectfully."
The rhetoric in the campaign had been fierce at times. Mr Van der Bellen had said he did not want Austria to be led by a "populist right-wing, pan-Germanic fraternity member" and even urged voters "who don't like me but perhaps like Hofer even less to vote for me".
Mr Hofer had been photographed sporting the German colours of the nationalist Marko-Germania student fraternity, which stands for "the German cultural community" and bears the slogan "Honour, Freedom, Fatherland".
At his swearing-in as Freedom Party candidate, Mr Hofer wore a cornflower in his lapel, which was a Nazi symbol in the 1930s.
Freedom Party campaign manager, Herbert Kickl, hailed Mr Hofer's performance, saying: "There are many Norbert Hofers in the Freedom Party and we are very, very well placed for parliamentary elections."
France's far-right National Front said: "This historic performance is certainly the precursor of future success for all patriotic movements, both in Austria and around the world."
But French PM Manuel Valls said in a Twitter post: "It's a relief to see the Austrians reject populism and extremism. Everyone in Europe must draw lessons from this."
Analysis: Jenny Hill, BBC News, Pinkafeld
While he awaited the result, the man who could have become the EU's first far right leader mowed his lawn. I met Norbert Hofer in his hometown - about an hour's drive from Vienna. We chatted at his garden gate - he was still perched on his ride-on lawnmower.
These elections, he said, would go down in history. And, he told me, whoever won would have to work to unite this country.
Perhaps he's right. The election has split Austria and exposed, once again, deep divisions in Europe - how to deal with the migrant crisis, the economy and how to balance national interests against those of the EU.
And what's characterised this vote - widespread disillusionment with the political mainstream and growing support for Mr Hofer's far-right Freedom Party - is reflected across a growing number of European countries.
Alexander Van der Bellen
Alexander Van der Bellen is the first environmental activist to become Austrian president. He is a chain-smoker and left-leaning liberal committed to the EU.
He is the son of aristocratic refugees from Russia's 1917 Bolshevik Revolution. First they escaped from Pskov to Estonia, then in 1940 they fled the Soviet occupation - the communist takeover of the Baltic states.
The family settled in Austria's Tyrol region. Alexander grew up in Kaunertal and does not speak Russian. His surname harks back to Dutch ancestry.
He studied economics at the University of Innsbruck and was later appointed professor at Vienna University. He retired from academia in 2009.
He was elected to parliament for the Greens in 1994, and from 1997-2008 was the party's spokesman.
In a TV debate, one of the few things he agreed on with Mr Hofer was that neither would accept TTIP - the free trade deal the EU is negotiating with the US.
What was the breakdown of the vote?
In nine out of Austria's 10 main cities Mr Van der Bellen came top, whereas Mr Hofer dominated the rural areas, the Austrian broadcaster ORF reported (in German).
Support for Mr Hofer was exceptionally strong among manual workers - nearly 90%. The vote for Mr Van der Bellen was much stronger among people with a university degree or other higher education qualifications.
Support for Mr Hofer among men was 60%, while among women it was 60% for Mr Van der Bellen.
What powers does the Austrian president have?
It is mostly a ceremonial post. But the president does have the power to dissolve the National Council - the more powerful lower house of parliament. That triggers a general election. The president can only do that once for a particular reason - he cannot use the same grounds to dissolve it again.
It is the chancellor's job to appoint government ministers. And the chancellor has the power to dismiss the government. But ministers have to be formally sworn in by the president.
Have you voted in the Austrian presidential election? If you are happy to discuss the election, email email@example.com with your story.
Please include a contact number if you are willing to speak to a BBC journalist. You can also contact us in the following ways: