Former minister and fiction writer Alvarado Quesada, 38, had 61 percent of the vote with results in from 91 percent of polling stations. His rival, Alvarado Munoz, a 43-year-old former TV journalist, had 39 percent of the vote.

Alvarado Quesada, who will be the youngest president in the modern history of Costa Rica, used the campaign to appeal to the progressive streak in a country known for pacifism and ecological stewardship. He has promised to allow gay marriages.

Alvarado Munoz, best known for religious dance songs and ballads, has vowed to restore what he calls traditional values by preventing gay marriages and restricting women’s access to abortions.

The election exposed deep divisions in the Central American tourist destination known for its laid-back beach culture and pristine rainforests, but whose rural communities remain socially conservative.

At a polling place in the western Pavas neighborhood of the capital San Jose, Alvarado Quesada, until recently a minister in the outgoing government also known for his student rock band, voted and spoke briefly to supporters.

“Costa Rica is an amazing country and we want to not only preserve its great democracy, its peaceful nature, its respect for the environment and human rights, but we also want to move Costa Rica forward,” he said.

Shortly after Alvarado Munoz cast his vote at a school in the capital he pledged to lead a government free of bias, in a possible sign that he had sensed his hardline stance was turning off centrist voters.

Costa Rican presidential candidate Fabricio Alvarado (C) of the National Restoration Party (PRN) addresses supporters following defeat in Costa Rica's run-off election on April 1, 2018 in San Jose. (Photo by AFP)

“We’re going to work for everyone and we won’t discriminate against anyone. We will protect all groups that have felt vulnerable ... people with disabilities, people with different sexual orientations,”he told reporters.

The two men took opposing positions on a January decision by the Inter-American Court of Human Rights, an influential regional body based in San Jose.

Fabricio, as supporters refer to Alvarado Munoz, called the ruling an affront to sovereignty. Threatening to remove the country from the court’s jurisdiction, he shot from the margins to win the first round of voting in February.

Quesada, by contrast, backed the court’s ruling. In the campaign's final debate, he called his opponent's comments homophobic.