A clear majority of German MPs have voted to legalise same-sex marriage, days after Chancellor Angela Merkel dropped her opposition to a vote.
The reform grants couples now limited to civil unions full marital rights, and allows them to adopt children.
Mrs Merkel's political opponents were strongly in favour. But the chancellor, who signalled her backing for a free vote only on Monday, voted against.
The bill was backed by 393 lawmakers, 226 voted against and four abstained.
The German legal code will now read: "Marriage is entered into for life by two people of different or the same sex", AFP news agency reported.
Following Friday's vote, Mrs Merkel said that for her marriage was between a man and a woman. But she said she hoped the passing of the bill would lead to more "social cohesion and peace".
Celebrations erupted after the vote, with social media timelines filled with rainbow colours.
As the vote was taking place German MPs tweeted pictures of their coloured voting cards, with blue to indicate a yes, a red for no, and white to abstain.
Firecrackers were left off in Parliament when the decision was announced, as the MPs who had voted for the bill hugged and congratulated each other.
One Twitter user posted: "Germany legalized same sex marriage. I am proud of Germany for the first time."
Another posted: "I might be crying some happy tears right now, marriage equality in Germany is everything I've wished for this month."
Another tweeted her excitement about the prospect of a family wedding: "My brother can finally marry his boyfriend of eight years."
How did the sudden vote come about?
During her 2013 election campaign, Mrs Merkel argued against gay marriage on the grounds of "children's welfare," and admitted that she had a "hard time" with the issue.
But in an on-stage interview with the women's magazine Brigitte on 26 June, she shocked the German media by saying, in response to an audience member's question, that she had noted other parties' support for gay marriage, and would allow a free vote at an unspecified time in the future.
The usually-cautious chancellor said she had had a "life-changing experience" in her home constituency, where she had dinner with a lesbian couple who cared for eight foster children together.
As the news spread on Twitter, supporters rallied under the hashtag #EheFuerAlle (MarriageForAll) - and started calling for a vote as soon as possible.
Mrs Merkel's current coalition partners - the centre-left Social Democrats (SPD), who are trailing Mrs Merkel's Christian Democrats (CDU) in opinion polls - then seized the political initiative.
They called for a vote by the time parliament went into summer recess at the end of the week - prompting Mrs Merkel to complain she'd been "ambushed".
Strong public support
A recent survey by the government's anti-discrimination agency found that 83 percent of Germans were in favour of marriage equality.
The day after the Republic of Ireland voted to legalise gay marriage in May 2015, almost every German newspaper splashed a rainbow across its front page.
"It's time, Mrs Merkel" Green party leader Katrin Goering-Eckhart said then. "The Merkel faction cannot just sit out the debate on marriage for everyone."
Germans go to the polls on 24 September, and continued opposition to a vote made Mrs Merkel risk looking anachronistic.
Mrs Merkel's coalition partners, the SPD, had ruled out a future coalition deal unless reform was agreed on. The Greens, the far-left Linke, and the pro-business Free Democrats took the same view.
The right-wing Alternative for Germany (AfD) is now the only party to oppose same-sex marriage.
But conservatives within Mrs Merkel's own CDU were against a change - as was the CDU's Bavarian sister party, the Christian Social Union (CSU), whose votes Mrs Merkel needs in the September election.
Commentators say this partly explains why she has rejected a vote on marriage equality until now - and why she was taken off-guard by the snap vote.
A host of European countries have beaten Germany to a same-sex marriage law.
Civil marriages are legally recognised in Norway, Sweden, Denmark (excluding the Faroe Islands), Finland, Iceland, the Netherlands, Belgium, Spain, Portugal, Luxembourg, France, the UK (except Northern Ireland and Jersey), and the Republic of Ireland.
But in Austria and Italy - as in Germany before Friday's vote - gay couples are restricted to civil partnerships.
New Zealand legalised marriage equality in 2013.
- BBC / RNZ