It has taken more than 25 years, divided two nations and been cause for protests great and small, but on Tuesday Greece and Macedonia finally declared peace.

After countless rounds of UN-mediated talks, the two Balkan neighbours announced that they had agreed to end the row over what to call the former Yugoslav Republic of Macedonia.

The tiny state will henceforth be known neither by its acronym, FYROM, nor simply as Macedonia but as the Republic of North Macedonia – a geographical qualifier that ends any fear in Athens of territorial ambition against the neighbouring Greek province of the same name.

“After months of negotiation we have managed to reach a deal that will solve our longstanding difference over the name of our neighbour,” said the Greek prime minister, Alexis Tsipras. “They have agreed to rename their country the Republic of North Macedonia, a change that will apply in their international and bilateral relations and domestically.”

The new name not only made a clear distinction between Greek Macedonia and the country’s northern neighbour, but put a decisive end to the irredentism the country’s erstwhile title had conveyed, he said.

“The deal that we have reached for the first time ensures that they do not have, and in the future can never claim, any relationship to the ancient Greek civilisation of Macedonia. I am deeply convinced that this agreement is a great diplomatic victory, but also a historic opportunity ... a historic moment for the Balkans and our peoples.”

The two neighbours have been at loggerheads ever since the former republic seceded from Yugoslavia and declared independence as the Republic of Macedonia. Fuelling fury in Athens, the new Slavic nation had laid claim to ancient Greek figures, not least the warrior king Alexander the Great.

Tsipras, whose left-led administration came to power in 2015, had made settlement of the issue a priority. Negotiations brokered by the US mediator Matthew Nimetz accelerated this year following the ascent to power of the Social Democrat Zoran Zaev in Skopje.

Zaev told a news conference timed to coincide with Tsipras’ address that the deal preserved the Macedonian ethnic and cultural identity. Both its language and people would continue to be known as Macedonian. The agreement would be put to popular vote in a referendum later this year, he said.

Tsipras and Zaev, both in their early forties with instinctively anti-nationalist reflexes, had long taken a progressive view on a dispute that had not only harmed bilateral ties but also held up Macedonia’s membership of the EU and Nato.

As evidence mounted of Russian mischief-making in the Balkans, the dispute had also fuelled growing fears of destabilisation in the region.

Nato’s secretary general, Jens Stoltenberg, was among the first to welcome what he described as a historic agreement and urged both sides to finalise it.

“This will set Skopje on its path to Nato membership, and it will help to consolidate peace and stability across the wider western Balkans,” he said in a statement.

In what will be seen as a moment of success for Europe, Zaev is expected to present the agreed settlement to the German chancellor, Angela Merkel, during talks in Berlin on Wednesday.

The European council president, Donald Tusk, tweeted: “Sincere congratulations to PM @tsipras_eu and PM @Zoran_Zaev. I am keeping my fingers crossed. Thanks to you the impossible is becoming possible.”

The two countries had been in a race to secure a solution before an EU summit in late June.

For both Tsipras and Zaev it has taken political courage to get this far. The nationalist backlash in both countries is likely to be fierce with opponents already deriding the accord as an act of treachery.

Greece’s main opposition leader, Kyriakos Mitsotakis, denounced the deal, arguing that it amounted to Athens accepting the existence of a Macedonian language and nation.

Officials on both sides, however, insisted that boldness was required if one of the most obdurate, if abstruse diplomatic rows was to be put to rest.

Stelios Koulouglou, an MEP with Tsipras’ Syriza party, said: “For far too long Greece had been ridiculed because of this dispute. In the future this will be a lesson in diplomacy in how to avoid turning a minor feud into a major international issue because of nationalism and its poisonous rhetoric.”