Minnesota is now projecting a $876 million surplus over the next two years, a stunning turnaround after a bruising partisan battle that shut down the government for 20 days over the summer.

The saga drew in national political figures as the state’s dilemma ignited a broader debate over how the country should approach government spending. Minnesota’s shutdown, the longest in recent history, locked Democratic Gov. Mark Dayton and the Republican-controlled legislature in an impasse over how to reduce the state’s projected $5 billion deficit.

GOP presidential candidates and governors urged state Republicans to dig in to push through deep cuts. State parks, libraries and the zoo were closed, along with the office that tallied how much the government was losing in revenue. Ultimately, Dayton conceded to their demands, and the shutdown ended when he signed a $35 billion budget that relied on deferred payments to schools and additional borrowing that would eventually eliminate the deficit.

As it turns out, cuts from previous budgets helped, but it was the state’s economic recovery that led to the surprise surplus.

The heroes? Minnesota businesses.

“Obviously the private sector deserves huge credit,” said Larry Jacobs, a University of Minnesota political scientist. “Minnesota has had a remarkable economy.”

But he added that previous budget cutting has played a big role, too: “We would not have a surplus if we had not had significant cuts in spending. The key point to note here is this is a surplus after sharp cuts over a long period of time.”

The news came as a huge surprise to many in Minnesota, who were girding for the state to forecast a budget deficit as high as $1 billion.

“This is kind of like we were transported from a horror house to a funhouse,” Jacobs said. “We went from a government shutdown over a $5 billion deficit to a situation that was entirely unanticipated.”

The surplus is the first forecasted for the state in four years.

In announcing good budget news, experts say Minnesota may not be alone: Many states are starting to show signs of a slow but steady economic recovery. “We’ve seen a lot of states that are meeting their forecast targets or hitting above them,” said Todd Haggarty, a policy analyst with the National Conference of State Legislatures. “Minnesota definitely does not stand out in that regard.”

A report his group published Thursday found just four states facing mid-year budget shortfalls, compared with 15 at this time last year. Across the country, states are averaging a 1.9 percent increase in revenue projected for 2012.

“Things are generally continuing to improve at a slow pace,” Haggarty said.

Minnesota officials, however, say their budget surplus is the result of factors unique to the state. For example, it has regained about a third of the jobs lost since the recession began. Nationally, that number stands at 22 percent.

It has also taken unusual steps to reduce its Medicaid spending. The state is one of four to expand its Medicaid program before the health reform law requires it to do so in 2014. That expansion has seen lower-than-expected enrollment, leading to lower costs.

“There were a number of changes those seem to have driven down costs more than were anticipated,” said Jay Kiedrow­ski, a former Minnesota commissioner of finance, now at the University of Minnesota.

Experts on Minnesota politics doubt the new surplus will have either party reevaluating its role in this past summer’s budget battle. Quite the opposite, members of both parties have already begun to claim credit for making policy changes they believe led to the surplus.

“They’re going to do a bit of a Fred Astaire dance step and make this work with their message,” said the University of Minnesota’s Jacobs. “This doesn’t put an end to the debate. It feeds into what is now a fairly established narrative.”