BERLIN — Germany's parliament backed the legalization of same-sex marriage on Friday in a historic vote hailed by gay activists and leftist parties but criticized by some in Chancellor Angela Merkel's ruling conservative bloc and by the Catholic Church.

The move brings Germany into line with many other European nations including France, Britain and Spain and follows Merkel's surprise decision this week to allow her lawmakers to follow their own conscience rather than the party line on the issue. Merkel, daughter of a Protestant pastor, voted against the bill.

Two men kiss as they attend a rally of gays and lesbians in front of the Brandenburg Gate in Berlin on June 30, 2017. Tobias Schwarz / AFP - Getty Images

Hundreds of gay activists, some with painted faces, celebrated outside the Bundestag lower house of parliament after the vote, waving rainbow flags and placards that read "Marriage for all - make love for all".

"This is simply a historic day for Germany," said Soeren Landmann, a marriage equality activist.

"Today, thousands of same-sex couples were given equality, and the two-class society in matters of love was abolished. Germany can really rejoice today."

The vote has particular resonance in Germany as it unwinds a legacy of virulent homophobia. Earlier this year parliament agreed to grant compensation to thousands of gay men jailed under a 19th century law that was strengthened by the Nazis and only dropped in 1969 when homosexuality was decriminalised in West Germany.

Merkel, who is seeking a fourth term in a national election on Sept. 24, said she had voted against the bill because she believed that marriage as defined under German law was between a man and a woman.

German Chancellor Angela Merkel cast her vote after a debate on same-sex marriage in Bundestag, Germany?s lower house of Parliament in Berlin on June 30, 2017. Tobias Schwarz / AFP - Getty Images

But she said her decision was a personal one, adding that she had become convinced in recent years that same-sex couples should be allowed to adopt children.

"I hope that the vote today not only promotes respect between the different opinions but also brings more social cohesion and peace," she said.

A survey by pollster INSA for daily Bild showed this week that three quarters of Germans favored the legalization of same-sex marriage.

Lawmakers voted by 393 votes in favor of same-sex marriage to 226 against, with four abstentions.

The landmark vote came about almost by chance after Merkel announced on Monday she would allow lawmakers to vote on same-sex marriage according to their individual conscience, drawing the ire of some in her traditionally Catholic conservative bloc.

ELECTION TOPIC

Same-sex marriage became a hot election topic after three parties -- the Social Democrats (SPD), the pro-business Free Democrats and the Greens -- each made it a condition for joining any future coalition with Merkel's conservatives, effectively forcing her hand. The SPD is part of the current coalition.

At an event hosted by women's magazine "Brigitte" this week Merkel spoke of a "dramatic experience" when she told a lesbian in her home constituency how she had struggled with the idea of same-sex couples adopting children.

Merkel said the woman had responded: "I tell you what, come and visit me in my home, where I live with my lesbian partner and eight foster children. The foster children have been with us for many years, and I think they are doing well."

The SPD then pushed for Friday's vote. The swift, untroubled passing of the legislation contrasts with the experience of neighbouring France, where the issue was a political hot potato for months, triggering mass street protests.

Erika Steinbach, an independent lawmaker who quit Merkel's Christian Democratic Union (CDU) in protest over her open-door policy towards asylum seekers, accused the chancellor of betraying the party's values in pursuit of electoral advantage.

"It runs against the CDU's own party programme, which sees marriage as being between a man and a woman, so CDU decisions are clearly not worth the paper they are written on. It would be hard to exaggerate how excruciating this is," said Steinbach.

The right-wing, anti-Islam Alternative for Germany (AfD) party, which opposed marriage equality, accused Merkel of "abandoning the last conservative nuances her party had".

The Catholic Church said it regretted the decision.

"An appreciation of same-sex cohabitation can also be expressed by a different institutional design," said Archbishop Heiner Kochof of Berlin.

Political analysts say the issue will likely have faded from voters' minds by the time the September election comes around.

The vote marks a rare victory for the SPD, who are trailing the conservatives in opinion polls. SPD General Secretary Hubertus Heil accused Merkel of political cowardice over her decision to make the issue a matter of private conscience.

"After 17 years with her at the top, the CDU has become cowardly," he told RND news. "Hers is a politics of no conviction."

The same-sex bill will likely be signed into law by President Frank-Walter Steinmeier some time after July 7.

For a graphic showing countries that have legalized same-sex marriage, visit this infographic.

Follow NBC Out on Twitter, Facebook and Instagram