The International Court of Justice has ruled that a group of disputed islets in the Caribbean are Colombia's, rejecting a claim by Nicaragua.
But the court also redrew the maritime border, extending the Nicaraguan area.
The decision potentially gives Nicaragua more access to fishing grounds, as well as reported underwater oil and gas deposits.
Colombia and Nicaragua have been at odds for years over the border, with tensions periodically flaring.
In its ruling on Monday, the ICJ, the UN's highest court, said the islets of Roncador, Quitasueno, Serrana, Serranilla, Bajo Nuevo, Cayo Bolivar and Alburquerque were Colombia's.
The court set new borders to give Colombia control of the water and seabed around its islands and islets.
But the new demarcation line also gives Nicaragua more sea territory.
"Colombia strenuously rejects this aspect of the ruling," said President Juan Manuel Santos, referring to the court's decision.
He described the court's decision to move the maritime border westwards as "wrong and contradictory".
"We won't discard any legal recourse or mechanism available under international law to defend our rights," he said after a meeting of the Colombian cabinet.
But the ICJ ruling is binding.
Hours after the decision, Mr Santos flew to the island of San Andres, where he planned to spend the night and meet local authorities, the BBC's Arturo Wallace said.
In Nicaragua, President Daniel Ortega said the decision should be celebrated by his compatriots.
"The court has given to Nicaragua what belonged to us: thousands of kilometres of natural resources."
The long running case has been before the ICJ since December 2001, when Nicaragua first filed its claim.
But the dispute goes back much further.
The competing claims date from the early 19th Century, when the nations of Latin America were gaining their independence from Spain.
Nicaragua and Colombia signed a treaty in 1928 to settle the border and sovereignty of islands in the Caribbean.
But in 1980, Nicaragua's Sandinista government unilaterally annulled the agreement, arguing that it had been signed under US pressure.
In 2007, the ICJ ruled that the treaty was valid and that the sovereignty of three islands, San Andres, Providencia and Santa Catalina, remained with Colombia.
The archipelago lies some 775km (480 miles) from the Colombian coast and 230km from Nicaragua.
The current border is on the 82nd meridian.
The ICJ ruling does not not affect the maritime borders of Costa Rica and Honduras.