(Getty)

Centre-left candidate Carlos Alvarado Quesada has been elected as President of Costa Rica, heavily defeating an anti-gay pastor who based his campaign on opposing same-sex marriage.

Fringe evangelical Fabricio Alvarado Muñoz, 43, received a surge in support after running on an aggressively anti-same-sex marriage platform, and went into the final run-off as the marginal favourite.

In contrast, Quesada, 38, has been outspoken in his support of gay rights and a pan-American human rights treaty that requires the country to provide legal recognition to same-sex couples.

And he defied the polls – most of which showed Muñoz level or ahead – to secure a commanding victory.

With 95 percent of the votes counted late last night, Quesada was leading by 60.8 percent to 39.2.

Speaking to thousands of cheering fans, the new President-elect said: “My commitment is to a government for everybody, in equality and liberty for a more prosperous future.

“There is much more that unites us than divides us.”

On Twitter, he wrote: “This campaign demonstrated the power of love. That is the strength that should unite us”.

He added: “I will lead a government for all and all. That shelters all people, without any distinction.”

Quesada, who leads the ruling Citizens’ Action Party, will become President in May, taking over from President Luis Guillermo Solis, who submitted the marriage equality petition that led to the current furore over same-sex marriage.

Muñoz conceded the race, reportedly falling to his knees, arms raised in front of his fans, some of whom were crying.

He said: “We are not sad, because we made history, because our message touched the country’s deepest nerves.”

The battle over LGBT rights came to the fore after the Inter-American Court of Human Rights ruled that Costa Rica and 15 other countries were violating human rights protections by preventing same-sex couples from marrying.

The court found Costa Rica is in violation of its treaty obligations under the American Convention on Human Rights (ACHR) by not providing marriage equality.

Following Muñoz’s strong performance in the first round, he declared the results a victory for the “traditional family”.

The candidate said: “We propose the sovereignty of the family as the fundamental basis of society.”

“Costa Rica has sent a message to traditional parties – never again will they meddle with the family.”

Meanwhile, Quesada had stood firm over his commitment to human rights laws.

He said: “The Costa Rica of the 21st century requires a government that knows how to move forward with vigor, love, happiness (and) the agenda of equality.”

While there has been much pan-American solidarity around the court ruling, the US has notably shied away from pressing Costa Rica over the issue, and has refused to take part in a statement welcoming the ruling.

The US was the only member of the Organisation of American States (OAS) LGBTI Core Group that refused to sign on to a statement supporting the court’s opinion.