Horst Koehler has been chosen for a second five-year term as Germany's president.
The re-election of Koehler hands a boost to the ruling Christian Democrats (CDU) of Angela Merkel, the German chancellor, in the run-up to parliamentary elections in September.
Koehler, a former head of the International Monetary Fund, received 613 votes from the 1,224 members of the federal assembly in the vote held on Saturday.
The result gave him victory by the narrowest majority in the first of three possible rounds of voting.
"Obviously I'm pleased he won in the first round," Merkel said.
"It's no secret that we're eager to have different majority in parliament [in September]. That was our goal here and we accomplished it. It's good news for Germany."
The CDU currently rules Germany in a grand coalition with the Social Democrats (SPD), who backed Gesine Schwan, a university president, in a bid to unseat Koehler.
Franz Muentefering, the chairman of the SPD, said he was a "proud of Gesine Schwan", who had hoped to become Germany's first female head of state.
"Every election has its own special dynamic"
"The vote went the way it went," he said.
Both parties are keen to end the partnership and Merkel hopes to form a government with the Free Democrats (FDP), who are currently in opposition, after the parliamentary election.
Merkel, who is due to begin her campaign for a second four-year term as chancellor, sought to calm expectations after Koehler's win.
"Every election has its own special dynamic," she said.
"The September election will have its own dynamic."
The CDU, along with its Bavarian CSU sister party, leads the SPD in opinion polls for September's election but is unsure of holding on to the chancellery because of complex coalition arithmetic.
If the conservatives and their preferred FDP partners fall short of a majority in the ballot as in 2005, the two most likely scenarios are another grand coalition or a coalition without Merkel made up of the SPD, the FDP and the Greens.
Koehler briefly addressed the federal assembly on Saturday about the economic challenges facing Germany.
"Our country is in the midst of a crisis hitting the whole world," he said. "We've got a lot of work ahead of us."
The German presidency is a largely ceremonial post.