The deal should spare Republicans the embarrassment of seeing the government shut down on their watch. But it also gave a glimpse of the reluctance of lawmakers to bend to Mr. Trump’s spending priorities, like his desire for sharp cuts to domestic programs, with the increase in funding for medical research a prime example. And it leaves the border wall looming as a fight in future spending negotiations, especially if Mr. Trump presses the issue, as he vowed to do during a rally Saturday night to mark his 100th day in office.

Details of the agreement were not yet public on Sunday night, but several congressional aides described key parts of it. The measure will cover the rest of the fiscal year, which ends Sept. 30.

Lawmakers had already taken action to keep the government open while they finalized the spending agreement. On Friday, Congress approved a one-week spending measure that averted a shutdown on Saturday.

In recent days, the spending talks on Capitol Hill had seemed unlikely to result in the kind of impasse that could lead to a shutdown, the last of which occurred in 2013. Some key obstacles, including the border wall and a standoff over subsidy payments to insurers under the Affordable Care Act, seemed to fall away as congressional negotiators worked on a deal. The White House said last week that it would continue to make the payments, and that message reassured Democrats who wanted to ensure that the payments, which go to insurers to lower deductibles and other out-of-pocket costs for low-income consumers, would not be cut off.

Lawmakers were able to reach a resolution in the spending package on another potential sticking point, the fate of retired coal miners who faced losing their health coverage, an issue that brought lawmakers close to a government shutdown in December. The deal provides a permanent extension of health coverage for the retired miners.