Angela Merkel, the German chancellor, has failed to get her candidate for president elected in the first two rounds of voting in a special assembly, forcing a final third round.
Having failed to secure an absolute majority in the first round, Christian Wulff, the choice of Merkel's coalition, secured 615 votes in the second round, short of the 623 needed for an absolute majority.
Joachim Gauck, the candidate of the opposition Social Democrats (SPD) and the Greens, won 490 votes in the ballot of 1,244 MPs and public figures.
The appointment of the country's largely ceremonial head of state is usually straightforward but a loss for Merkel's candidate could push her embattled government to the brink of collapse.
"If Wulff were to lose, it would surely amount to an enormous loss of face," Nils Diederich, a political scientist at Berlin's Free University, told the AFP news agency.
Alan Fisher, Al Jazeera's correspondent in Berlin, said the voting showed "there had been a challenge to her [Merkel's] authority".
Gauck, a former East German dissident and pastor, is popular among the public and speculation has been rife that he could precipitate a political crisis.
Luc Jochimsen, the little-known nominee of the Left Party, Germany's third opposition group, took 123 votes in the second round.
A simple majority for any candidate will suffice in the third round which will be held later on Wednesday following discussions among the political parties.
'Unpredictable secret ballot'
The ballot follows the resignation of Horst Koehler as president on May 31.
Koehler said he was stepping down after being criticised for an interview he gave on the German military's role abroad.
His resignation was seen as a major blow to Merkel, adding to her own bumpy second-term start.
"The unpopular Greece-euro decisions ... and the budget cuts, the disappearance of a president - handling this would be difficult for anyone," political scientist Gerd Langguth wrote in the Welt am Sonntag newspaper last weekend.
"And this week, the threat of an unpredictable secret presidential ballot comes on top of it all," he wrote.
The job of president is largely symbolic in Germany, as the head of state serves as a kind of moral arbiter.
But Koehler was popular, and Germans do care about who represents them as a shadow leader behind Merkel.
Since 2003, Wulff has governed the state of Lower Saxony, home to automaker Volkswagen. At 52, he would become Germany's youngest-ever president, if elected.
Favourite to take over from Wulff in Lower Saxony is David McAllister, who is half Scottish.