The president in Moldova is elected by parliament and not by popular vote.
Nicolae Timofti, the candidate put forward by Moldova's ruling pro-Western alliance, gathered one more vote than the 61 required for his election.
Three lawmakers who were formerly part of the Communist opposition and one independent lawmaker voted for Timofti, enabling him to secure victory.
Timofti, a 63-year-old judge with no apparent political allegiances, was the only candidate running for the post.
Addressing parliament before the vote, Timofti said he strongly supported Prime Minister Vlad Filat's efforts to steer Moldova closer to Europe.
"We need an idea that can unite society and help it move forward," he said, "and I think this idea is European integration."
Timofti also pledged that he would "not take part in politics" and would represent all Moldovan citizens. He said he would strive for a settlement of the dispute over Transdniester, a strip of land on Moldova's eastern border controlled by pro-Russian separatists.
The squabbling parties forming the ruling Alliance for European Integration had, until now, been unable to reach a compromise with the group of Communist defectors and had failed to get a candidate elected in previous votes.
Deputies from the Communist Party, which has been demanding the dissolution of parliament, boycotted the March 16 vote.
Moldova has had acting presidents since Vladimir Voronin of the Communist Party ended his second presidential term more than 900 days ago.
The March 16 election took place amid a heavy police presence.
The Communist Party called on its supporters to hold a peaceful rally outside the parliament in Chisinau to protest the election. Pro-government demonstrators announced plans for a counterrally.
Voronin rejected the vote and accused the three defectors of "treachery." At least 70,000 communist protesters marched through the streets of the capital, Chisinau, shouting "Down with the usurpers!"