ByFrancois Murphy, ReutersMay 23, 2016

A man walks between election posters of Alexander Van der Bellen, candidate for presidential elections and former head of the Austrian Greens, right, and Norbert Hofer, candidate for presidential elections of Austria's right-wing Freedom Party, FPOE, left, in Vienna, Austria, Monday, May 23, 2016. Mr. Van der Bellen narrowly won with 50.3 percent of the vote.

VIENNA — Austria has elected a 72-year-old former leader of the Greens party to be its next president, narrowly avoiding becoming the first country in the European Union to vote in a far-right candidate as head of state.

After an election on Sunday that was too close to call, Austrian officials spent most of Monday counting hundreds of thousands of postal ballots which ended up vaulting Alexander van der Bellen past Freedom Party rival Norbert Hofer and into the ceremonial post of president.

The Interior Ministry gave van der Bellen 50.3 percent of the vote, compared to 49.7 percent for Hofer, who had run on an anti-immigration platform.

"Of course I am sad today," he said. "I would have liked to take care of our wonderful country for you as president."

Hofer's defeat averts an embarrassing setback for Europe's political establishment, which is increasingly under threat from populist parties that have profited from concerns about the region's refugee crisis and years of weak growth and high unemployment.

"It's a relief to see the Austrians reject populism and extremism," French Prime Minister Manuel Valls said in a Twitter post. "Everyone in Europe must draw lessons from this."

Austria is a relatively prosperous country, but it has been at the center of a record influx of migrants from the Middle East, fanning public resentment towards the two centrist parties - the Social Democrats (SPO) and the conservative People's Party - that have dominated politics since the end of World War Two.

The Second Republic successfully portrayed Austrians as the first victims of Nazi horror, while denying the roles of so many as loyal Nazis. Later, "Austria was treated by the Allies as a liberated and occupied country at the same time," [Professor Anton] Pelinka says, "liberated from Nazi Germany but occupied like Germany."

And then as domestically Austria became a fully Western democracy, officially it declared its neutrality in 1955. "Internationally it was neither East nor West," says Pelinka. "It's a rather convenient position. No one dares touch [it]. But it's not an active position."

Michal Vašečka, a Slovak sociologist and professor at Masaryk University in Brno in the Czech Republic, says that this matters today because the countries that were under the Austro-Hungarian empire never "properly reconciled with Nazism, Fascism, or the results of World War II," he says. Because of this, unlike Germany where the far-right is growing but remains on the fringes, hateful rhetoric finds more fertile ground here.

Sunday's provisional result, which did not include the postal ballots, showed Hofer ahead with 51.9 percent to van der Bellen's 48.1 percent.

But the SORA institute, a pollster, had said that mail-in ballots were likely to favor van der Bellen because they are traditionally used by more educated voters. The institute's election-day polling showed 81 percent of voters with a university degree had backed van der Bellen and 86 percent of workers voted for Hofer.

The vote in Austria, a country of 8.5 million people, had unsettled leaders elsewhere in Europe, particularly in neighbor Germany where a new anti-immigration party, the Alternative for Germany (AfD), is on the rise.

In France, the National Front of Marine Le Pen is leading in polls ahead of a presidential election next year. Across the Channel, the UK Independence Party is campaigning for Britain to leave the European Union in a referendum on June 23.

Hofer, 45, has described himself as a center-right politician and told voters not to believe suggestions from other parties that he would be a dangerous president.

But his party has its roots in Austria's Nazi past, a history the country has not confronted as openly as Germany.