The deal passed with 153 votes in the 300-member parliament, two more than the minimum needed. It has faced fierce opposition in both countries, and recently cost Prime Minister Alexis Tsipras his parliamentary majority after a small right-wing party quit the governing coalition in protest. It passed with the support of independent lawmakers.
Tsipras' left-wing Syriza party holds 145 seats in parliament, six short of a majority. "Today is a historic day," Tsipras said. "Greece is safeguarding an important part of its history, its heritage of ancient Greek Macedonia. Today we are writing a new page for the Balkans."
Under the Prespa Agreement — dubbed after the border lake where it was signed last June — Macedonia changes its name to North Macedonia, and Greece drops its objections to the country joining NATO and eventually the European Union.
With the Greek parliament's endorsement, the agreement is almost complete although some procedural steps remain, including Greece signing its northern neighbor's NATO accession protocol. "Congratulations my friend Alexi Tsipras, together with our peoples we reached a historical victory," Macedonian Prime Minister Zoran Zaev tweeted.
The Macedonian government issued a statement congratulating the people of both countries. "The Balkans, Europe and the world have received a new model for building friendships and cooperation between nations and states," it said.
The deal's passage was quickly welcomed by both NATO and the European Union. Friday's vote was "an important contribution to the stability and prosperity of the whole region," NATO Secretary-General Jens Stoltenberg tweeted. "I look forward to the future Republic of North Macedonia joining NATO."
United Nations Secretary-General Antonio Guterres also praised Greece, saying the agreement would help promote reconciliation efforts in Europe and beyond. U.N. deputy spokesman Farhan Haq said Guterres looks forward to completing the process outlined in the agreement, which was negotiated under U.N. auspices.
The ratification came after three days of acrimonious parliamentary debate and numerous street protests, some of which turned violent. A rally last Sunday saw tens of thousands of people gather outside parliament, with clashes erupting between groups of demonstrators and riot police.
Torrential rain and driving wind kept many protesters away on the final day of the debate. Scores of demonstrators who braved the weather conditions outside parliament chanted "traitors" as lawmakers voted inside.
Top EU officials said the agreement's approval has "written a new page of our common EU future." European Commission President Jean-Claude Juncker, the EU's top diplomat and the senior official supervising the bloc's enlargement said it "took political courage, leadership and responsibility on all sides to resolve one of the most entrenched disputes in the region."
Greece has long argued use of the term Macedonia implied territorial claims on its own northern province of the same name. The issue has been a contentious one for decades, stymieing repeated rounds of U.N.-mediated negotiations and whipping up nationalist and patriotic sentiment in both countries.
Opposition has been particularly fierce in the northern Greek region of Macedonia, which borders the republic that claimed the same name after declaring independence from the former Yugoslavia in 1991. Critics claimed the deal signs away their identity and a cultural heritage dating back to Alexander the Great more than 2,300 years ago.
More than 150 people have been detained for questioning since Thursday following violence at demonstrations against the deal in Athens and two towns in northern Greece. Most were released without charge.
At least two lawmakers from the governing the Syriza party have said their homes were targeted by violent demonstrators, while another was targeting in a firebombing that caused no injuries. Greece's tourism minister, an independent conservative who supports the government, said she had received multiple death threats.
U.S. Secretary of State Mike Pompeo welcomed the Greek parliament's move, praising the government's "vision, courage and persistence" in pushing ahead with the historic agreement. He said it would improve regional stability and allow Macedonia to take its "rightful" place in NATO with the new name of North Macedonia.
Britain described the vote as a "historic moment bringing a decades-old dispute close to an end." "Today's vote paves the way to the full implementation of the agreement, which has the full support of the U.K.," Foreign Secretary Jeremy Hunt said in a statement released by his office. "Both countries have demonstrated great courage and the will to work together to affect real change."
Derek Gatopoulos in Athens, Konstantin Testorides in Skopje, Macedonia, Lorne Cook in Brussels and Danica Kirka in London contributed to this report.