With 99% of the vote counted, Zoran Milanovic, a former Croatian prime minister, had 53% while Kolinda Grabar Kitarovic, the country's first female head of state when she won five years ago, had 47%.
The result is a blow for the ruling conservatives at a time Croatia holds the European Union's rotating presidency and before a parliamentary election later this year.
Milanovic's team and supporters at his election headquarters were jubilant but he was cautious not to be triumphant.
"I know I am not everyone's favorite, there are many people who didn't want me to be president," he said.of Croatia's deep division between the left and right, promising to be the president for all.
"I hope and I believe that I will not betray the trust you have given me," Milanovic told cheering supporters. "I have no illusion, I am not promising mega-powers, I know what president can and may do."
Apparently shaken, Grabar Kitarovic congratulated Milanovic, but unlike her opponent insisted on a nationalist message, referring to a united Croatia based on the war in the 1990's that followed the country's split from the former Yugoslavia.
"This is the Croatian state, created in blood, defended in blood and carried in love," said Grabar Kitarovic in a trembling voice. "Let it remain so."
Croatia assumed the EU's presidency on Jan. 1 for the first time since joining the bloc in 2013. This means that the EU's newest member state will be tasked for six months with overseeing Britain's divorce from the bloc on Jan. 31 and the start of post-Brexit talks.
Milanovic's win is a rare victory for a left-wing official to a major post in central Europe, where populists and conservatives have been winning elections in recent years.
Grabar Kitarovic and Milanovic made it into the runoff on Sunday after the first round of voting on Dec. 22.
Milanovic won slightly more votes than Grabar Kitarovic in the first round. There are 3.8 million voters in Croatia, a country of 4.2 million that is also a member of NATO.
The two candidates represent the two main political options in Croatia. Grabar Kitarovic was backed by the governing, conservative Croatian Democratic Union, a dominating political force since the country declared independence in 1991. Milanovic enjoyed support from the leftist Social Democrats.
Even though Croatia's presidency is largely ceremonial, Sunday's election was an important test before a parliamentary election expected later this year. Milanovic's victory could rattle the conservative government during the crucial EU presidency and weaken its grip on power in an election year.
Support for Grabar Kitarovic has ebbed following a series of gaffes in the election campaign. The 51-year-old had a career in diplomacy and in NATO before becoming Croatia's first female president in 2015. Going into the runoff, Grabar Kitarovic evoked Croatia's unity during the 1991-95 war in a bid to attract far-right votes.
The 53-year-old Milanovic is hoping to regain some clout for liberals in the predominantly conservative nation where the Roman Catholic Church holds significant influence.
Prone to populist outbursts while prime minister, Milanovic lost popularity after the ouster of his government in 2016. He now says he has learned from the experience and matured.
Although Croatia is a member of the EU, it still has corruption problems and economic woes — issues not resolved since its devastating 1991-95 war to break free of the Serb-led Yugoslav federation.