U.S. stocks rose in frenetic trading Wednesday, scoring their first back-to-back gains since February, after lawmakers and the White House reached an agreement on a $2 trillion stimulus package.

Investors have been eager to see the government commit to further aid for the economy as the growing coronavirus pandemic has shut factories, sent students home from universities and upended travel for millions of Americans. The pending legislation is likely to include direct financial payments to many Americans, as well as loans to…