Central American countries have reached a deal to allow some of the thousands of Cuban migrants stranded in Costa Rica for more than a month to continue onwards to the United States.

The agreement, which was reached in Guatemala City on Monday, will come into effect in January.

Under the deal, an undisclosed number of Cuban migrants will be flown to El Salvador and then ferried to Mexico by bus.

"The solution emerging is an absolute exception and only for those people who entered national territory legally," said Manuel Gonzalez, Costa Rica's foreign minister.

The pilot programme will be subject to revision as diplomats are expected to work out logistical details in upcoming meetings.

Officials from Panama, El Salvador, Guatemala, Honduras, Belize, Mexico, Costa Rica and the International Organization for Migration took part in the talks.

Cuban migrants wait to be accommodated in hotels in Panama as they look forward to move to Costa Rica [Marcelino Rosario/EPA]

Surge in Cuban migrants

An estimated 8,000 Cubans have been stuck in Costa Rica after Nicaragua shut its borders in mid-November.

A close ally of Cuba, Nicaragua had accused Costa Rica of sparking a "humanitarian crisis" after issuing transit visas to more than 1,000 Cubans.

Under current US policy, any Cuban who reaches US soil is allowed to stay and pursue permanent residency.

Those, however, who are intercepted at sea by the US coastguard are sent home, leading many to attempt the difficult overland journey through third countries.

The surge in Cubans is largely motivated by worries that their preferential US asylum rights could be revoked after the restoration of diplomatic relations between Havana and Washington last year. 

On Sunday, Pope Francis urged countries in the region to resolve the "humanitarian drama" in Costa Rica quickly.

Source: Agencies