(This version of the June 17 story corrects academic institution of expert in penultimate paragraph)

PRESPES, Greece/BITOLA, Macedonia (Reuters) - Greece and Macedonia set aside three decades of dispute on Sunday as they agreed on a new name for the former Yugoslav republic, paving the way for its possible admission to the European Union and NATO.

The foreign ministers of the two countries signed an accord to rename the former Yugoslav republic the “Republic of North Macedonia”, despite angry protests on both sides of the border over a deal seen as a national sellout by some on both sides.

In the idyllic setting of Prespes, a lake region that borders Greece, Macedonia and Albania, leaders from the two countries embraced and shook hands in the presence of European and United Nations officials.

The agreement still requires the approval of both parliaments and a referendum in Macedonia. That approval is far from assured, as it faces stiff opposition from the Greek public, and Macedonia’s president has vowed to block the deal.

“Very few believed we would be able to leave behind 26 years of unfruitful dispute,” said Greek Prime Minister Alexis Tsipras, who survived a no-confidence vote mounted by the opposition in parliament on Saturday.

“We have a historic responsibility that this deal is not held in abeyance,” Tsipras said as he and his Macedonian counterpart Zoran Zaev received a standing ovation.

In the Macedonian capital Skopje, police fired stun grenades and tear gas on Sunday night to disperse a protest rally by several hundred nationalists.

A Reuters witness saw protesters pelting police with stones, chanting “Macedonia, Macedonia we will give our lives for Macedonia.” Some of the demonstrators were arrested.

Slideshow ( 15 images )

Across the border, up to 70 percent of Greeks object to the name compromise, an opinion poll by the Proto Thema newspaper showed on Saturday. In Psarades, the tiny lakeside community where the deal was signed, the church bell tolled in mourning, draped in a Greek flag.

Some 30 km (20 miles) away in the Greek village of Pisoderi, about 3,000 people rallied against the deal and at least six were injured in clashes with police who fired tear gas to disperse an angry crowd on a hillside.

“We don’t accept anything, we don’t recognize anything. For us none of it is valid,” said Costas Venetikidis, a protester. “Macedonia is in our soul, that’s why we’re here.”

Not far from the Greek border in the Macedonian city of Bitola, thousands protested draped in national flags, chanting “This is Macedonia.”

“This shameful deal will not pass. We will defend Macedonia’s name and pride,” said Petre Filipovski, 40.

‘WE HAVE MOVED MOUNTAINS’

Following the break-up of Yugoslavia in 1991, Greece’s northern neighbor took the name Macedonia. Athens refused to accept it, saying it implied territorial claims over the Greek province of Macedonia and an appropriation of ancient Greek culture and civilization.

Zaev, who arrived from across the lake on a speedboat, said the two neighbors had “moved mountains” by reaching the accord. It was “a dignified solution acceptable to both sides”, he said.

Slideshow ( 15 images )

Veteran UN mediator Matthew Nimetz, who has overseen talks for a quarter-century, described the agreement as a fair and honorable deal. It was, he said, an example of “how neighbors can solve a problem if they really work at it”.

“Today is my birthday,” said Nimetz, 79. “I told my family this year I don’t need any gifts because two prime ministers are going to give me a big gift.”

Athens had blocked Macedonia’s hopes of joining the EU and NATO, objections it must now lift under the deal.

Others might still object.

“One big concern is Russia. Moscow has noticeably refused to endorse the agreement,” said James Ker-Lindsay, senior visiting fellow at the London School of Economics. “It knows that this will see Macedonia join NATO,” he said.

“Given recent allegations of Moscow’s involvement in other elections and referendums, this will be a real concern for NATO and the EU.”

(This version of the June 17 story corrects academic institution of expert in penultimate paragraph)