The 69-28 vote leaves government funding at current levels through Feb 18, and gives Democratic President Joe Biden plenty of time to sign the measure before funding was set to run out at midnight on Friday.
The Senate acted just hours after the House of Representatives approved the measure, by a vote of 221-212, with the support of only one Republican.
Congress faces another urgent deadline right on the heels of this one. The federal government is approaching its $28.9 trillion borrowing limit, which the Treasury Department has estimated it could reach by Dec 15. Failure to extend or lift the limit in time could trigger an economically catastrophic default.
"I am glad that in the end, cooler heads prevailed. The government will stay open and I thank the members of this chamber for walking us back from the brink from an avoidable, needless and costly shutdown," Democratic Senate Majority Leader Chuck Schumer said on nailing down a deal with Republicans to clear the way for passing the bill.
The vote ended weeks of suspense over whether Washington might be plunged into a government shutdown at a time when officials worry that the potentially dangerous omicron variant of COVID-19 could take hold in the United States after being discovered in South Africa.
Such a shutdown could have forced layoffs of some US government medical and research personnel.
Senate Democrats defeated an attempt by a handful of conservative Republicans to attach an amendment that would have prevented enforcement of Biden's coronavirus vaccine mandate for many US workers.
Republican Senators Mike Lee, Ted Cruz and Roger Marshall had earlier raised the possibility that the government could partially shut down over the weekend while the Senate moves slowly toward eventual passage.
"It's not government's job, it's not within government's authority to tell people that they must be vaccinated and if they don't get vaccinated, they get fired. It's wrong. It's immoral," Lee said before the defeat of the amendment.
Over the past few days, Senate Minority Leader Mitch McConnell insisted there would be no government shutdown from congressional inaction. But he had to work through the day on Thursday to get his Republican lawmakers in line on a deal allowing quick passage of the funding bill.
The emergency legislation is needed because Congress has not yet passed the 12 annual appropriations bills funding government activities for the fiscal year that began on Octn1.
A partial government shutdown would have created a political embarrassment for both parties, but especially for Biden's Democrats, who narrowly control both chambers of Congress.
The fact the temporary spending bill extends funding into February suggested a victory for Republicans in closed-door negotiations. Democrats had pushed for a measure that would run into late January, while Republicans demanded a longer timeline leaving spending at levels agreed to when Republican Donald Trump was president.
"While I wish it were earlier, this agreement allows the appropriations process to move forward toward a final funding agreement which addresses the needs of the American people," House Appropriations Committee Chairwoman Rosa DeLauro said in a statement announcing the agreement.
But she said Democrats prevailed in including a $7 billion provision for Afghanistan evacuees.
Once enacted, the stopgap funding measure would give Democrats and Republicans nearly 12 weeks to resolve their differences over the annual appropriations bills totalling around $1.5 trillion that fund "discretionary" federal programmes for this fiscal year. Those bills do not include mandatory funding for programmes such as the Social Security retirement plan that are renewed automatically.