Nicolae Timofti, 63, a relatively politically neutral figure, was put forward by the ruling Alliance for European Integration to break the impasse caused by communist opposition to its previous candidate, an Alliance leader.
He won over three communist defectors to bolster the three-party ruling coalition's 59 votes, giving him 62 votes in the 101-seat chamber.
"After 917 days, the epic of electing a president is over," parliamentary commission chairman Tudor Deliu told deputies.
Veteran Communist leader Vladimir Voronin angrily denounced "treachery" by the three defectors, who quit the party last November, and tens of thousands of communists marched through the city centre, shouting "Down with the usurpers!"
Police said 70,000 massed near the main government building while the communists put their number at 100,000. Special forces in riot-gear ringed the parliament building itself during the voting session and patrolled side streets.
The EU's foreign policy chief Catherine Ashton, in a joint statement with Štefan Füle, the EU's enlargement commissioner, said the vote would open up dialogue in the country.
"This paves the way for an inclusive dialogue between all political factions with a view to responding further to the economic and social aspirations of the country and consolidating Moldovan society," the statement issued on Friday read.
Wedged between Ukraine and European Union member Romania, with which it shares a common language, Moldova is one of Europe's poorest states with an average salary of $270 (€205) per month.
It looks to wine and vegetable exports and inflows of cash from thousands of Moldovans working abroad to sustain an economy that is heavily reliant on Russian energy imports.
But despite its poverty, Moldova, which has a population of 4 million, is pressing for association status with the EU and has received plaudits from Brussels for its economic reform plans.
In a swift reaction to the election, Romanian President Traian Băsescu said he had assured Timofti of Romania's support for reform in Moldova and for moving closer to the EU.
A Romanian foreign ministry statement said his election was "a guarantee for continuing the internal reform process and commitments made in relations with the European Union."
A new leadership could also help resolve the status of Transnistria, a strip of land on Moldova's eastern border controlled by pro-Moscow separatists for the past 20 years.
Transnistria, which has no international recognition as an independent territory, itself elected a new leader last December, increasing prospects of a long-term settlement.
Igor Dodon, leader of the three-man socialist group which broke communist ranks, announced they would support Timofti after he had presented his program, saying his election was a better option than pushing for early parliamentary elections.
"We must put an end to the crisis and get down to work for the good of the country," he told the parliamentary session.
In his speech before the vote, Timofti pledged to maintain strategic ties with the United States, Russia and Germany, as well as strive for a settlement of the Transnistria problem.
"Moldova must become a bridge between East and West from which it can only win," said Timofti, a judge since 1976 and head of Moldova's Supreme Magistrate Council, a governing body of the court system.
He pledged to combat large-scale systemic corruption which is rife in Moldova as in several of the former Soviet republics.
"I am aware that I will become the president of the poorest country in Europe, consequently I consider it a question of principle to lead the fight on corruption," he said.
Tension was high in the run-up to the parliamentary vote and the authorities brought the session forward to morning from afternoon to wrong-foot the communists.
The building was the scene of violent protests in April 2009 after a parliamentary election in which the communists won 50% of the vote, enough to allow them to select a new president and amend the constitution.
The ruling Alliance emerged from the ensuing chaos to push the communists, led by Voronin, into the opposition. But their strength meant it was never able to get its candidate - centre-left politician Marian Lupu - elected president.