The 60-year-old conservative was nominated to become the first woman to hold Brussels' top job last month by the leaders of the bloc's 28 member states, to the annoyance of many MEPs.
The Strasbourg parliament would have preferred a candidate chosen by one of its political groups, but in the end a small majority -- 383 members of the 751-member assembly -- voted for her.
She will now replace Jean-Claude Juncker as head of the EU executive on November 1, one day after Britain is due to leave the union, and serve a five-year term.
"The task ahead of us humbles me. It's a big responsibility and my work starts now," the polyglot mother-of-seven told lawmakers, thanking all members "who decided to vote for me today."
"My message to all of you is let us work together constructively, because the endeavour is a united and strong Europe," she said, urging capitals to nominate an equal number of men and women to join her commission.
If von der Leyen had lost, Brussels faced a summer of infighting instead of preparing for Brexit, battling Italy over its debt and confronting Hungary and Poland over threats to democratic values.
- 'A majority is a majority' -
==============================
At a news conference after the vote, von der Leyen played down the narrowness of her win, noting that a "majority is a majority" and acknowledging that some members had opposed the nomination process.
"Two weeks ago I didn't have a majority because no one knew me. There was a lot of resentment because I wasn't a lead candidate," she admitted, adding that she was happy to build a majority so quickly.
"It's a good base to start with," she said. The veteran minister will head briefly to Berlin on Wednesday to say farewell to her government and the German armed forces, then return to work on building an administration.
In Berlin, Chancellor Angela Merkel praised her long-time ally as a "committed and convincing European" who would "tackle with great vigour the challenges facing us as the European Union".
The president of the European Council of EU leaders, Donald Tusk, also congratulated von der Leyen, having previously declared "she will be a passionate fighter for Europe's unity."
From the left, Spanish Prime Minister Pedro Sanchez also offered his congratulations, but said von der Leyen must push for "a more social, fair, sustainable and feminist Europe."
Von der Leyen has had only a short time since the 28 EU leaders nominated her to win over the main centre-right EPP, socialist S&D and liberal Renew Europe blocs she hoped would get her the necessary 374 votes.
In the hours between her speech and the start of voting, party officials suggested she could count on the centre-right, almost all of the liberals and maybe two-thirds of the left.
The election was by secret ballot, but the tight margin of victory suggested that she had only won over the pro-European centre -- and that several mainstream members abstained.
- Top jobs package -
====================
The nominee announced Monday that she would step down from Angela Merkel's German government this week whatever happened in the vote, underlining her European ambitions.
Juncker received 422 votes five years ago, and Brussels-born von der Leyen's 383 was seen as disappointing in comparison, given she was backed by figures like France's President Emmanuel Macron.
Von der Leyen's nomination was part of a package of so-called "top jobs" and outgoing Belgian premier Charles Michel is still in line to head the European Council of EU leaders.
IMF director Christine Lagarde's appointment to the European Central Bank also remains on course, and on Tuesday the French former finance minister announced her resignation from the global body.
In addition to Brexit and the other issues facing the EU, member states are also wrangling over new rules for distributing migrants and refugees after Italy's populist government began to prevent rescue boats from landing.
For that, the commission president will need a reliable majority in Strasbourg, after this year's elections threw up a more fragmented EU parliament than ever.