Rebel votes in the ruling centre-right coalition twice deprived Christian Wulff of an outright win against challenger Joachim Gauck.
But Mr Wulff won the third round with 625 votes to 494 for Mr Gauck.
Although the role of president is largely symbolic, the protracted vote is seen as embarrassing for Mrs Merkel.
She is under increasing pressure over her government's package of austerity measures and because of infighting in the cabinet, the BBC's Steve Rosenberg in Berlin says.
Although her candidate won, she remains under pressure, he adds.
It was only the third time in German post-war history that a presidential election has gone to a third round.
Mr Wulff will succeed Horst Koehler, who resigned a month ago.
Huge cheers
In the first round, the Lower Saxony state governor and deputy leader of Mrs Merkel's Christian Democratic Party was 23 ballots short.
In the second round, he fell eight votes short of an absolute majority.
Mr Gauck, a non-partisan civil rights campaigner put forward by the centre left, appeared close to tears as his supporters applauded his result in the third ballot, which needed a simple majority.
But the biggest cheers came from Mrs Merkel's supporters when it was clear Mr Wulff had won.
In the special session of the parliament, 1,244 representatives could take part. Half were federal legislators and half state-parliament nominees.
Much of Germany's media saw the longest presidential vote in German history - stretching into around nine hours - as a setback for the chancellor.
The daily Handelsblatt called it a "debacle" and Die Zeit called the result a "humiliation" for the government.
Bild, the mass-circulation daily, said it was a "massive slap for the ruling coalition".
Source