WASHINGTON—A two-year agreement to raise federal spending and at the same time lift the government’s borrowing limit will go to the president’s desk after it passed the Senate with more Democrats voting for it than Republicans.

The bill, which provides for more than $2.7 trillion in discretionary spending over the next two years, was the result of weeks of negotiations between Treasury Secretary Steven Mnuchin and House Speaker Nancy Pelosi (D., Calif.). It suspends the debt ceiling until the end of July 2021 and sets spending...