By Gilberto Lopes
BBC Mundo, Costa Rica
It has been a nail-biting finale that has kept Costa Ricans nervously waiting for election results.
It has been the hardest fought election since 1966
As midnight approached, the difference between the two leading candidates got smaller and smaller with each report by the Supreme Electoral Tribunal.
The party mood in the NLP headquarters, at a hotel in the capital San Jose, turned into expectation and astonishment as the official results showed an unforeseen outcome.
With more than 80% of the votes counted, centrist Oscar Arias from the National Liberation Party (NLP) had only a slight advantage (0.3%) over Otton Solis from the Citizens' Action Party.
Far from the easy victory that opinion polls had predicted for Mr Arias, the two main candidates were neck and neck.
It has been the hardest-fought electoral contest since the 1966 vote, when Jose Joaquin Trejos unexpectedly won over the NLP's Daniel Oduber.
The main issue that has drawn a line between the two main contenders is their support for the Central American Free Trade Agreement (Cafta) with the US, which is set to come into effect later this year.
What is in play
Mr Arias supports Cafta, while his rival Mr Solis would like to renegotiate some terms of the agreement.
Central American countries signed the deal in 2004, but Costa Rica is the only nation which has not yet ratified it.
Costa Ricans have taken part in a wide debate on the effects of the pact, and are deeply divided on the issue.
But it seems that in recent months opposition against Cafta has grown stronger, and this was reflected in Sunday's election.
Although NLP is likely to win clear a majority in congress, a tie in the presidential contest could lead to an impasse over Cafta.
No-one now dares to anticipate the outcome of the ballot.
But, whatever the results, there is a sense of astonishment among voters. It seems that Costa Rica could join the Latin American countries which have rejected the free-market policies favoured by the US.