On March 31, after months of tense negotiations, a bipartisan group of eight senators hoping to overhaul the nation’s strained immigration system felt as if they were finally closing in on a deal. Mr. Schumer, a New York Democrat and member of the group, was preparing to go on the NBC program “Meet the Press” to hail the progress.

Instead, a half-hour before Mr. Schumer was to appear, the office of Senator Marco Rubio, a Florida Republican seen as a crucial member of the group, e-mailed a news release, its title in all capital letters: “No Final Agreement on Immigration Legislation Yet.”

Mr. Rubio and the others eventually reached a deal, and the resulting legislation was to be introduced this week. But the story of how the senators struck an agreement is one of intense across-the-aisle bargaining in an age when compromise is hard to reach. It was built on unlikely friendships, survived near stalemates and involved an apology in the form of a cheese pizza.

Mr. Schumer said he was caught completely off guard by Mr. Rubio’s missive. “I was surprised,” he said. “I was blindsided.”

Everyone in the group seemed to respect Mr. Rubio — he came to meetings prepared and engaged, and his colleagues all believed he was operating in good faith. But suddenly, many of the members were worried that after all their hard work, Mr. Rubio might bolt.

So Mr. Schumer, who by then had the cellphone numbers of his seven fellow group members memorized and on speed dial, called Mr. Rubio. The two talked for 90 minutes. By the time the call was over, Mr. Schumer knew that Mr. Rubio was in.

“He convinced me that he really wanted to get a bill done but he really needed some space,” Mr. Schumer said.

When Senator Richard J. Durbin of Illinois, the No. 2 Senate Democrat and a member of the group, first approached Mr. Rubio in the Senate gym about joining the others, Mr. Durbin said he told him, “The ticket to entry there is commitment to a path to citizenship” — a must-have for the Democratic senators.

Mr. Rubio said he was open to putting citizenship on the table, but had some requirements of his own. He said strict border security requirements and other enforcement measures would have to be in place — to ensure that there was not a new wave of illegal immigration, and that no one was rewarded for breaking the law — before any illegal immigrants could attain permanent legal status.

The group, which began meeting after Thanksgiving, was glad to have Mr. Rubio on board. Both Mr. Durbin and Mr. Schumer were considered fairly liberal Democrats, and Senator Lindsey Graham of South Carolina and Senator John McCain of Arizona, Republican members of the group, were viewed with skepticism by the conservative base.

An error has occurred. Please try again later.

You are already subscribed to this email.

Senator Mitch McConnell of Kentucky, the Republican leader, had told Mr. Graham and Mr. McCain that Mr. Rubio, who was elected in 2010 on the Tea Party wave, should be part of any negotiations.

Relationships were forged slowly and in unlikely places. Although Mr. Durbin and Mr. Rubio were already friends as part of the 6 a.m. crowd in the Senate gym, Mr. McCain and Mr. Schumer had never had a strong relationship, despite having overlapped in the Senate for more than a decade.

“Getting McCain back into the game — he was a little leery about Schumer,” Mr. Graham said.

But the two men managed to bond. During a trip to the Arizona border with some of the group members, Mr. McCain came down with food poisoning and had to cancel immigration meetings he had scheduled later that day. Mr. Schumer filled in as he made himself at home in Mr. McCain’s Phoenix office (he ate jelly beans from the jar on Mr. McCain’s desk) and told groups in Arizona to trust their senator.

“You need to have McCain’s back,” Mr. Schumer said. “He is being really brave here, and doing the right thing.”

The border trip took place over Passover. So Mr. McCain presented Mr. Schumer, who is Jewish, with chocolate-covered matzo.

Still, there were rough patches. In one heated meeting, a top Democratic immigration policy adviser yelled at another Senate aide, but Mr. McCain thought he was yelling at him. Mr. McCain exploded — and then bought the adviser a cheese pizza as an apology after he realized his mistake.

At another point, after months of negotiations and just before the Easter recess, a deal for an agricultural worker program that could prove crucial in bringing reluctant Republicans senators in farm states on board came together only at the last minute. (It was negotiated by Mr. Rubio and Senator Michael Bennet, a Colorado Democrat and member of the group; and two senators who were not members, Dianne Feinstein, Democrat of California, and Orrin G. Hatch, Republican of Utah.)

When the group finally agreed on the guest worker program — a crucial component of the overall bill — at the end of March, Mr. Schumer called Denis R. McDonough, the White House chief of staff, to share the news. Mr. McDonough, who had been receiving regular updates and knew how tense the negotiations had gotten, let out a “Whooo!”

A version of this article appears in print on April 17, 2013, on Page A10 of the New York edition with the headline: Unlikely Friendships, Stalemates and a Pizza Apology. Order Reprints|Today's Paper|Subscribe