Democrats and Republicans narrowly averted a partial shutdown of the federal government Friday night, agreeing on a budget deal and a short-term funding extension little more than an hour before the clock struck midnight and time ran out.