Early in the day, it seemed that the more liberal members would prevent Republicans from scoring political points on the issue. Senate leaders avoided debate on a comprehensive measure proposed by the GOP, substituting it with a more narrowly tailored crackdown — on welfare and unemployment benefits — that passed mostly along party lines.

But Wednesday would be the start of a dizzying two days of nearly round-the-clock deal-making, as the Senate made its way through hundreds of amendments. Immigration was a prominent, but ultimately just one part, of the deliberations.

Murray, despite her key role, seemed more ambivalent. On Tuesday, she told reporters that Republicans were targeting a problem that did not exist, because the state already checks applicants before awarding public benefits.

“Why are we letting the Republicans spot us on issues that we were with them on?’’ he said.

Steven A. Baddour, a Methuen Democrat who helped draft the final measure, said there was no reason to continue battling Republicans when many senators agreed with many of the provisions anyway.

Supporters said the measure reflected broad public consensus on reserving state services for legal residents; liberal members of the Senate said they felt blindsided and ashamed that the body had passed such a strict measure so abruptly.

And with a 28-to-10 vote, the Senate came back Thursday and passed a sweeping measure that would toughen or expand rules that bar illegal immigrants from obtaining public health care, housing, and higher education benefits. It would set up hot lines for anonymous tips about illegal immigrants holding jobs and encourage the state attorney general to consult with the US Justice Department to enlist more state resources to halt illegal immigration.

In the end, the Republicans, who make up just five of the Senate’s 40 members, got more than they wanted, just hours after the Senate appeared to have killed, or at least slowed down, their own plan to tighten immigration laws.

Murray, who had shown little appetite for a headline-grabbing crackdown on illegal immigrants, wanted a new strategy. She set up a late-night meeting with three other senators, where — over strawberry cream pie, cranberry-lime seltzer, and M&Ms — the group, mindful of a new poll showing overwhelming public support for barring illegal immigrants from state services — drafted a stricter crackdown than even the most conservative advocates believed possible.

That’s when Senate President Therese Murray banged her gavel — temporarily freezing action on the floor and effectively changing the course of the immigration debate in Massachusetts.

It was late Wednesday night, hour 11 of a grueling state budget debate, and senators were arguing over another contentious amendment on illegal immigration. Republicans were hammering Democrats for going too soft. Leading Democrats were quietly pushing to make a deal to quell the criticism.

Republicans called it a “poison pill.’’ Conservative and moderate Democrats promised they would strengthen the measure as debate went on, with a series of amendments. Liberal senators seemed confident they could hold their ground, and had the votes to supplant 14 Republican amendments as they came up with measures of their own that would simply put existing practices into law — and demonstrate to the public that most state services were already off-limits to illegal immigrants.

“It would have been a long debate, but I think we would have defeated the Republican amendments,’’ said state Senator James B. Eldridge, an Acton Democrat.

But some Democrats did not want the perception that they were unwilling to take stronger action against illegal immigration, and they talked throughout the day with their Republican colleagues. Republicans feared Democrats would pass only a few narrow measures and then claim they had tackled the issue.

“I was serious in expressing frustration,’’ said state Senator Bruce E. Tarr, a Gloucester Republican.

The behind-the-scenes debate continued into the night, and then broke into public view, when another immigration measure came up for debate.

“There was a big brouhaha by the Republicans,’’ said state Senator Steven C. Panagiotakos, a Democrat who chairs the Ways and Means Committee. “The minority party kept criticizing it: ‘How could we depend that anything is going to get done on this area?’’’

Murray, with Baddour lobbying her on the dais, had heard enough.

“She literally banged the gavel and Bruce [Tarr] was in the middle of his speech,’’ Baddour said. “And she called a group together and said, ‘We need to work this out.’ ’’

When business ended at 10 p.m., Murray called Tarr, Panagiotakos, Baddour, and three staff members into her office, staying past midnight to draft the new policy.

Many rank-and-file Democrats knew nothing about the new proposal until the next day, when they were given the bill during a closed party caucus, minutes before they were asked to vote on it. Some on the left were shocked and outraged, such as Sonia Chang-Diaz who told colleagues she was “embarrassed’’ by the Senate’s efforts, according to two members who were present. Eldridge said it was so hastily written that technical errors nearly excluded tens of thousands of legal residents from public housing.

“I just don’t understand why we would sit down at the table in the kind of posture that we did in the end, as a Democratic caucus, with a five-member [Republican] minority,’’ Chang-Diaz said yesterday. “There were perfectly good alternatives for us in terms of reassuring the public.’’

But with a Suffolk University/7 News poll of Massachusetts residents showing 84 percent of respondents opposed to state services for illegal immigrants, and a tough election coming in fall, most Democrats signed on.

Still, the measure remains far from a done deal. Both the House, which narrowly defeated a related measure last month, and Governor Deval Patrick, who has been skeptical of further immigration crackdowns, would need to sign on before it becomes law.

“I don’t necessarily agree with everything that was in it,’’ Murray told reporters Thursday. “But that was the will of the body.’’

Noah Bierman can be reached at nbierman@globe.com.

Correction: Because of a reporting error, an earlier version of this story misstated the hometown of state Senator James Eldridge. He is from Acton.

© Copyright 2010 Globe Newspaper Company.