WASHINGTON — Ebullient Democrats assumed control of the House on Thursday and elected Representative Nancy Pelosi of California speaker, returning her to a historic distinction as the first woman to hold the post. They then moved to defy President Trump and passed bills that would open government agencies shuttered by an impasse over his insistence on funding for a border wall. Both measures are almost certain to die in the Senate.

On the first day of divided government in a reordered Washington, Ms. Pelosi, now second in line to the presidency, and Mr. Trump clashed from their respective ends of Pennsylvania Avenue almost from dawn until dusk.

The California Democrat began her day by suggesting that a sitting president could be indicted. Late in the day, Mr. Trump made an attention-getting appearance in the White House briefing room with a belligerent demand for a wall on the border with Mexico, drawing a rebuke from the newly installed House speaker, who said she would give no more than a dollar to fund what she branded “an immorality.”

In between, as the start of the new House showcased a younger and more diverse majority in the staid corridors of the Capitol, Ms. Pelosi pledged to run a “unifying” Congress that would bridge partisan divides and heal rifts in a polarized country.