Lebanon's parliament has elected Michel Aoun as president, ending a political stand-off that has left the post empty for more than two years.
The newly-appointed president, a Maronite Christian, vowed to protect Lebanon from "regional fires" - a reference to the conflict in Syria.
Mr Aoun was backed by the powerful Shia Islamist group, Hezbollah.
His candidacy was blocked by the rival, Sunni-dominated Future Movement until a deal was struck earlier this month.
It will reportedly see the Future Movement's leader, Saad Hariri, become prime minister.
Mr Aoun, an 81-year-old former army general, secured 83 votes in the 128-seat chamber when MPs convened for their 46th attempt to choose a head of state.
The stalemate has paralysed Lebanon's government, which is struggling to deal with deep divisions over the five-year civil war in neighbouring Syria and the arrival of more than one million refugees.
Lebanon has been without a head of state since Michel Suleiman stepped down at the end of his term in May 2014.
The country's complex power-sharing system stipulates that the president should always be a Maronite, Lebanon's largest Christian denomination. The prime minister is a Sunni Muslim and the speaker of parliament a Shia Muslim.
An Iranian official said the choice of Mr Aoun was "a great triumph for the Islamic Resistance movement in Lebanon and for Iran's allies and friends," Iran's Tasnim news agency reported.
It was also "a remarkable achievement" for Hezbollah, Ali Akbar Velayati, a senior adviser to Iran's supreme leader, added.
UN Secretary General Ban Ki-moon welcomed Mr Aoun's election and said he hoped that Lebanese parties would now continue to work in a spirit of unity and in the national interest.
The US State Department described Mr Aoun's election as "a moment of opportunity," and said it looked to all parties to uphold Lebanon's international obligations.
In the first round of voting on Monday, Mr Aoun failed to secure a two-thirds majority among the 127 MPs present.
A second round of voting - in which he required a simple majority to win - saw 128 ballots cast, which meant a third round was required.
The situation turned in Mr Aoun's favour on 20 October when Mr Hariri said he would support his candidacy in order to "protect Lebanon, protect the [political] system, protect the state and protect the Lebanese people".
Mr Aoun, who leads the Free Patriotic Movement, rose to prominence during Lebanon's civil war as an anti-Syrian commander of the Christian-dominated Lebanese Army.
In 1988, then-President Amin Gemayel appointed him head of an interim military government, just before his own presidential term expired without a successor having been elected.
However, Muslim leaders refused to recognise Mr Aoun's administration and backed the existing Sunni-led government.
Mr Aoun subsequently declared a "war of liberation" against Syrian forces in Lebanon.
Although a new president was elected in November 1989, Mr Aoun did not step aside until Syrian-led forces drove him out of the presidential palace and into exile in France in October 1990.
He returned to Lebanon only after Syrian troops withdrew in 2005 under international pressure following the assassination of former Prime Minister Rafik Hariri, Saad Hariri's father.
Despite his life-long opposition to Syria's influence, Mr Aoun formed a surprise political alliance with its staunch ally, Hezbollah, in 2006.
He also sided with Hezbollah in supporting Syrian President Bashar al-Assad, who is a member of the heterodox Shia Alawite sect, in his attempts to put down a Sunni-dominated rebellion.
The Saudi-backed Future Movement opposes Hezbollah's intervention in Syria as well as the group's military power inside Lebanon.