Pelosi, 80, faced a scare when five fellow Democrats defected and voted "present" or for someone else during the floor vote.
But the woman who is third in line to the presidency secured her fourth -- and perhaps final -- non-consecutive term as House speaker by earning 216 votes versus 209 for Republican leader Kevin McCarthy.
In a symbolic gesture beginning the 117th Congress, McCarthy formally handed the speaker's gavel over to Pelosi, who raised it in triumph as Democrats cheered and applauded.
"We begin the new Congress during a time of extraordinary difficulty," Pelosi told the chamber, noting the toll of 350,000 dead and 20 million infected by Covid-19.
"Our most urgent priority will continue to be defeating the coronavirus," a masked Pelosi said. "And defeat it, we will."
The vote took hours, as lawmakers were required to vote in groups of several dozen due to social distancing rules imposed during the coronavirus pandemic.
Pelosi has been Trump's chief nemesis in Congress, and the two clashed bitterly over the past two years, particularly as she leveled impeachment charges against the president.
Trump was impeached in December 2019, but the Senate acquitted him early in 2020. His successor, Joe Biden, takes office on January 20.
With Pelosi scrambling to keep her post, it was a handful of progressive lawmakers and members-elect who have been critical of her leadership but ultimately voted for her on Sunday.
Among them was Alexandria Ocasio-Cortez, one of the most high-profile Democrats in Washington, and incoming lawmaker Cori Bush, who is the first African-American woman to represent Missouri in Congress.
There are normally 435 members of the House, but just 427 votes were cast Sunday as a few congressmen-elect are in quarantine due to Covid-19 and a tight House race in New York has yet to be formally decided.
One congressman-elect from Louisiana, Luke Letlow, died of complications from Covid-19 last week, days before he was due to be sworn in.
That leaves Democrats with one of the slimmest House majorities in two decades, 222 to 211 with two vacancies, as they navigate the remaining few weeks of Trump and carry into the Biden presidency.
On a day that was anything but conventional, Republican Chip Roy interrupted the swearing-in process to ask that the delegations from the six swing states where Trump has contested the election results not be seated Sunday as House members.
Pelosi dispensed with the effort by holding a vote that affirmed her right to swear in all members.