Christian Wulff, the ruling center-right coalition candidate and candidate of Chancellor Angela Merkel, has been declared the president of Germany after three tense rounds of assembly voting.
In the final ballot, Wulff secured 625 votes, enough to secure a simple majority in the special electoral assembly.
At 51, he is Germany's youngest president, which is a largely symbolic role.
But while supporters of Wulff gave the winning candidate a standing ovation when the result was announced, the closeness of the vote was viewed as a sign of weakness for Merkel and her government.
Since Merkel's coalition took office in October it has been plagued by incessant political squabbling and was forced to push through new unpopular austerity measures in the wake of the eurozone debt crisis.
Public support for Merkel's government recently hit a 10-year low.
The presidential election went to a third ballot after Wulff, a state governor, failed to secure an absolute majority in the first and second rounds.
On paper, the center-right coalition had enough of a majority to secure victory for Wulff, but he fell 20 votes short of the 623 needed to win the first round. When he fell eight votes short in the second round, a third round of voting was held.
In a move that suggests widening fissures in the coalition, several center-right members broke ranks to vote for the opposition in the secret ballot, depriving Wulff of a decisive victory.
His nearest rival was Joachim Gauck, a former East German dissident, human rights activist, and pastor. Nominated by the Social Democrats and the Greens, he won 499 votes in the first round, dropped nine in the next, and finished with 494 in the final round.
Polls showed that Gauck -- who for a decade ran a commission investigating the role of East Germany's Stasi secret police -- had greater popular support than Wulff, prompting media speculation about a possible upset.
A third opposition candidate, Lucrezia Jochimsen, won a little over 120 votes in each of the first two ballots. The Left Party candidate withdrew before the final round.
Speaking to The Associated Press after the vote, Merkel brushed off doubts about her coalition's unity. She said, "What counts is that we now have a new president, and a president who was elected with absolute majority."
She added, "Christian Wulff will be a wonderful representative for this country."
The presidential vote was held to replace Horst Koehler, who stepped down from the post last month, just one year into his second term, citing public criticism over an interview he gave on Germany's military role abroad.
The German presidency carries a five-year term.
compiled from agency reports