The deeply divided parliament took four rounds of voting to elect Aoun, whose supporters flooded streets and squares across the country to celebrate his victory.
In Beirut's majority-Christian neighbourhood of Ashrafiyeh, supporters launched fireworks and loosed volleys of celebratory gunfire, as Aoun took the oath of office before lawmakers.
But analysts have warned his election will not be a "magic wand" for Lebanon, which has seen longstanding political divisions exacerbated by the war in neighbouring Syria and has struggled to deal with an influx of more than a million Syrian refugees.
The 81-year-old former army chief had long eyed the presidency, and his candidacy was backed from the beginning by Shiite movement Hezbollah, his ally since a surprise rapprochement in 2006.
But the key to clinching the post was the shock support of two of his greatest rivals: Samir Geagea, leader of the Christian Lebanese Forces, and Sunni former premier Saad Hariri.
Hariri, who Aoun is expected to appoint prime minister, said his endorsement was necessary to "protect Lebanon, protect the (political) system, protect the state and protect the Lebanese people".
Lebanon's political divide has been deepened by the war in neighbouring Syria, where Hezbollah and its allies back President Bashar al-Assad, even dispatching fighters to bolster his forces.
Hariri and his political allies firmly oppose Assad, as well as Hezbollah, which they accuse of seeking to monopolise power in Lebanon.
The divisions meant lawmakers were repeatedly unable to reach consensus on a candidate for president, a post reserved for a Maronite Christian and elected by parliament.
Aoun made reference to the war next door in remarks before parliament, saying Lebanon had so far "been spared the fires burning across the region."
"It remains a priority to prevent any sparks from reaching Lebanon," he said.
"We must also resolve the issue of Syrian refugees, so that they can return quickly, so that refugee camps do not turn into areas outside the control of the state," he added.
Lebanon has struggled with the influx of refugees, who have tested the country's limited resources, as well as the patience of its four million citizens.
Signup to our newsletter and follow us on Facebook and Twitter!
Many are eager to avoid a repeat of the country's experience with Palestinian refugees, hundreds of thousands of whom live in squalid and often lawless camps decades after they first came to Lebanon.
Lebanon's 127 lawmakers took nearly two hours to elect Aoun, who failed to secure a two-thirds majority in the first round, triggering a second.
The second round was repeated twice, after 128 ballots -- exceeding the number of MPs -- were cast.
At times the session threatened to descend into farce, with lawmakers casting votes for pop star Myriam Klink and "Zorba the Greek".
Other lawmakers, including from speaker Nabih Berri's bloc, cast blank ballots in protest at the horsetrading that secured Aoun's candidacy.
"A blank ballot is an objection to the way things were done," MP Ali Khreis told AFP before the vote.
"This country doesn't run on bilateral or trilateral agreements -- we believe in dialogue."
'DREAM COME TRUE'
But Aoun eventually secured 83 votes, far more than the 50-percent-plus-one majority needed for a second-round victory.
After announcing Aoun's win, Berri said the election "should be a beginning, not an end".
"This parliament is ready to extend its hand to lift up Lebanon," he said.
Analysts have cautioned that despite the unexpected accord on Aoun, Lebanon's political landscape remains deeply divided and the formation of a government is likely to be a difficult process.
And it remains unclear if the country's perpetually ineffectual political class can solve problems that citizens cite as key, like a trash collection crisis that has seen rubbish pile up in open dumps.
The parliament that elected Aoun has twice extended its own mandate, avoiding elections, because of disagreements over a new electoral law.
But for Aoun's supporters, the atmosphere was one of untrammelled joy.
"I'm so happy. After 25 years our dream has come true," said 33-year-old accountant Giselle Tammam, celebrating in Jdeideh outside Beirut.
"I can't believe it."