Congressional leaders agreed late Friday to a compromise that will keep the federal government funded for the remainder of the fiscal year — averting a government shutdown less than an hour before it was set to start.

House Speaker John A. Boehner (R-Ohio) announced the deal just before 11 p.m. The agreement came together in a few frantic hours at the near-deserted Capitol, with a midnight deadline looming.

“I’m pleased that Senator Reid and I and the White House have been able to come to an agreement that will, in fact, cut spending and keep our government open,” Boehner said at an impromptu news conference, mentioning Senate Majority Leader Harry M. Reid (D-Nev.).

Shortly after, President Obama read a statement from the White House, pointing out that the Washington Monument, seen lit up over his shoulder, would be open as usual on Saturday.

“Today, Americans of different beliefs came together,” Obama said. He said the cuts would be painful but necessary to maintain the country’s fiscal health. “We protected the investments we need to win the future.”

To keep the government running through Friday, lawmakers approved a short-term spending measure overnight — the Senate at 12:20 a.m. and the House at 12:40 a.m. — and said the final agreement should be approved next week.

If that happens, the measure would cut $37.8 billion from the federal budget through the end of September, congressional aides said.

Democrats had wanted to cut billions less: they assented to the larger figure, and in return Republicans dropped a demand to take federal funds from the group Planned Parenthood, according to aides in both parties.

However, Republicans did win the inclusion of a policy rider that forbids public money from going toward abortion procedures in the District of Columbia, a restriction that had previously been enacted when Republicans held power in federal Washington. The deal also adds money for one of Boehner’s favored projects, a program that provides low-income District students with money to attend private schools.

After 11 p.m., Reid described the negotiations briefly in a speech on the Senate floor.

“We didn’t do it at this late hour for drama, we did it because it’s been very hard to arrive at this point,” Reid said. “Both sides have had to make tough choices. But tough choices is what this job’s all about.”

The cuts, if enacted, would add up to the largest budget reduction for federal agencies in U.S. history. Some conservative Republicans had pushed for much more and grumbled about the compromise Friday.

But this was still a compromise made on their terms — and a sign of their power. Inside a few months, an ascendant Republican Party has managed to impose its small-government agenda on a town still largely controlled by Democrats.

“It’s all that one-half of one-third of the government can hope for,” said Rep. Darrell Issa (R-Calif.), emerging from a caucus meeting after the deal was struck. “In democracy, you compromise.”

The budget agreement is not yet law. On Friday, there simply wasn’t time to make it legal before midnight.

Some conservatives have signaled that they would be unhappy with any compromise that offered a smaller spending reduction than the $61 billion the GOP-led House had passed.

Cutting “$61 billion was just a modest first step,” said Rep. Jim Jordan (R-Ohio), co-chair of the conservative Republican Study Committee. He said he would vote “no” on the stopgap measure, signalling his displeasure that Boehner had settled for less. “You need to stay focused on getting the victories in the smaller things so you can get victories in the bigger things later.”

But it’s unclear how widespread such unhappiness will be inside the GOP. Already, many Republicans have called for the party to finish this fight, and focus on a much more ambitious one over the 2012 budget. This week, the House Budget Committee approved a plan that, over 10 years, would save $6 trillion.

The fight over the 2011 budget “is the first bite of the apple,” said Rep. Paul Ryan (R-Wis.), the budget committee chairman and the architect of that proposal. “We want to get billions in savings and then we want to move on to get trillions in savings.”

On Friday, Obama, who had called congressional leaders to the White House three times in the previous two days to hash out an agreement, played a less public role in the debate. Aides said he spoke on the phone to Boehner twice and Reid at least once, as well as huddling with his own advisers.

He canceled a Friday morning speech in Indianapolis on energy and later in the afternoon opted against a family trip to Williamsburg as a shutdown loomed.

With an agreement, Congress seemed to have resolved a battle that had been brewing since last fall’s elections. As the shutdown ticked closer, it had transfixed and partially paralyzed a vast federal bureaucracy.

If the government had closed, it would have meant closures at national parks and federal agencies, a halt to trash pickup in the District, and furloughs for more than 800,000 governments workers. Just preparing for that had slowed federal business to a crawl in the last week.

Now, officials said, museums should re-open in the morning, and government workers should come to work as scheduled. Washington should continue as if nothing happened.

This budget fight involved just a tiny fraction of the $1 trillion-plus that Congress doles out every year. But the timing was more important than the numbers. This was the first battle since Republicans took the House of Representatives, promising to pare back government spending and deficits. So Republicans — led by Boehner, in his first intense engagement as a leader — were determined to stand their ground in their first fight.

Democrats, on the other hand, still hold the Senate and the White House. In the Senate, Democratic leaders were determined not to be outmaneuvered by Republicans. And in the White House, President Obama seemed interested in cementing his role as a calm mediator, a CEO.

And none of them wanted to be the first to blink. That might have set a damaging precedent for future fights with higher stakes, over the decision to raise the national debt limit, and to pass a 2012 budget.

Their brinksmanship lasted even into Friday, the last day before a shutdown that would likely have damaged both parties’ political fortunes.

Democrats used the day to repeatedly bash the other side for refusing to budge on an issue tied to abortion.

“The House leadership, with the speaker, have a very clear choice to make, and they don’t have much time to make that choice,” Senate Majority Leader Harry M. Reid (D-Nev.) said in an afternoon news conference. “They can keep their word and significantly cut the federal deficit, or they can shut down the American government over women’s access to health care. If that sounds ridiculous, that’s because it is ridiculous.”

On the Republican side, Boehner emerged repeatedly to reiterate the same message: there was no deal yet.

“Most of the policy issues have been dealt with,” Boehner said Friday afternoon. “The big fight is over the spending.”

As the debate went on, frustration bubbled on both sides. Reid said the drawn-out negotiations had even provoked an outburst from Vice President Biden as a White House talking session hit snags Thursday night.

“Joe Biden wasn’t flustered,” Reid said when a reporter described Biden that way. “But he was damn mad.”

A key sticking point, aides said, was the argument about federal funding for groups such as Planned Parenthood — which was really an argument about abortion.

Planned Parenthood is the nation’s largest abortion provider. In accordance with federal law, none of the money it gets from the government funds abortions. But the organization receives millions of federal dollars for non-abortion services for low-income people, including breast exams and Pap smears, cholesterol and blood pressure screenings, family planning and contraceptives.

Conservatives have questioned the integrity of the group and argued that even if federal funding does not pay for abortions, it frees up other money that does.

In lieu of a provision defunding Planned Parenthood, Republicans this week proposed an alternative that would change the way funding for women’s health programs is distributed, according to senior congressional aides.

Instead of providing the money in federal grants to groups such as Planned Parenthood, the funds would be sent to states in the form of block grants. It would be up to state governments to distribute those funds to health groups.

Rep. Chris Smith (R-N.J.), who co-chairs the Pro-Life Caucus, said the original legislation stripping Planned Parenthood of all its federal funding would have been a blow to the group’s abortion efforts.

He said that one-fourth of all abortions in the United States were performed at the group’s clinics and that its services for women were minimal compared with community health centers that focus on helping the poor.

“You would think this is about women’s health care,” Smith said dismissively of the Democratic arguments. “It’s about abortion.”

Staff writers Perry Bacon Jr. and Felicia Sonmez contributed to this report.