It came really close, but New Jersey's state government won't shut down for the second year in a row.

Gov. Phil Murphy and fellow Democrats who control the state Legislature reached a state budget deal Saturday evening, capping more than four months of drama.

The deal to raise taxes on the wealthy and corporations in New Jersey averts a government shutdown that could have closed state parks and beaches, DMV offices, courts, state offices, racetracks, and eventually Atlantic City casinos. Budget battles forced a three-day shutdown last year and an eight-day shutdown in 2006.

The talks went right down to the wire as Murphy's administration prepared contingency plans for a closure, which he could order if no budget is in place when the clock went past midnight Saturday to start the fiscal year.

"Let me be clear: There will be no shutdown," Murphy said at a news conference outside his office in Trenton shortly after 7:30 p.m. with state Senate President Stephen Sweeney and state Assembly Speaker Craig Coughlin.

The Senate and Assembly will vote on the $37.4 billion spending plan and new taxes at 8 a.m. Sunday.

The governor and lawmakers had scrambled to put together alternative tax plans in hopes of reaching a compromise, but for weeks they remained far apart on the issue of taxes.

As part of the agreement, taxes on income over $5 million will increase from 8.97 percent to 10.75 percent and corporations with net income over $1 million will face a four-year tax hike.

"This is not a win for any of us individually," Murphy said. "This is a win for the middle class and working families and for those who look up and dream to be in the middle class all across New Jersey."

The gross income tax hike will hit more than 1,700 of New Jersey's wealthiest resident and nonresident taxpayers.

Corporations will pay a 2.5 percent surtax this year and next, followed by 1.5 percent in years three and four before phasing out entirely.

The deal spares New Jerseyans the three-eighths percentage point increase in the sales tax Murphy had fought for as part of his budget plan and legislative offers to double the Realty Transfer Fee on high-end home sales to 2 percent and expand the sales tax to include short-term rentals.

Sweeney, D-Gloucester, had fought Murphy hard on the matter of a millionaires tax, saying those high-income taxpayers were hammered by federal tax reforms that capped state and local tax deductions at $10,000.

Murphy had wanted that higher marginal tax rate to kick in at $1 million.

The deal brings to a polite end to a weeks-long war of words between Sweeney, a seasoned lawmaker and union ironworker, Murphy, a former Wall Street banking executive, and Coughlin, D-Middlesex, the rookie Assembly speaker.

"We had honest, blunt, sometimes heated yet always civil discussions," Murphy said. "I have tremendous respect for the leaders, individually and as partners, in governing. I appreciate their willingness to sit down and get this agreement done."

Sweeney called the weeks-long budget fight "one hell of a journey."

"Changing course in New Jersey is never easy," he said.

NJ Advance Media staff writer Brent Johnson contributed to this report.

Samantha Marcus may be reached at smarcus@njadvancemedia.com. Follow her on Twitter @samanthamarcus. Find NJ.com Politics on Facebook.