ALBANY – Senate Republicans struck a deal this afternoon with a group of five breakaway Democrats to form a coalition that will keep the GOP in at least partial control of the 63-member chamber in January.

The deal by the GOP and the Independent Democratic Conference appears to make moot a legal battle over a still-contested Senate race in the Mohawk and Hudson valleys that Democrats believe they will win if all the remaining 877 contested paper ballots are opened.

The deal amounts to a “Hail Mary” play by the Republicans, who suffered several stinging losses last month and already had to cut a deal with an incoming Brooklyn Democrat to cross party lines to caucus with them.

On paper at least, the arrangement would appear to also end the looming chaos that would be created if a new legislative session began in January without a clear party in control of the Senate.

Insiders say Gov. Andrew M. Cuomo privately expressed pleasure at the prospect that the Republicans, with whom he has been cozy for two years, worked out a deal with the group led by Sen. Jeff Klein, a Bronx Democrat who helped organize the defection by his now-five member group from the main body of Democrats two years ago.

The move came only hours after Sen. Malcolm Smith, a Queens Democrat, announced he was joining the breakaway group known as the IDC. Smith briefly led the Senate as majority leader before a series of bizarre leadership fights in 2009.

Klein and Senate Majority Leader Dean Skelos called their deal today “historic” and said it that will guarantee a “fiscally responsible, fully functional Senate.’’

“The Senate Republican conference has worked closely with the Independent Democratic Conference and Cuomo to bring historic progress to New York,” Skelos said in a statement. “We’ve brought spending under control, ended Albany dysfunction, and consistently delivered the bipartisan results New Yorkers need and deserve – even on many of the most difficult issues. Sen. Klein has proven to be a thoughtful and effective leader, and I look forward to partnering with him to move this state forward.’’

Klein, who has been tagged as a traitor by some of his Democratic colleagues in the Senate, said he looks forward to working with Republicans in a “constructive, bipartisan way.’’

Three weeks ago, Klein told The Buffalo News that his IDC group wanted to become a permanent caucus in the Senate, a position he said is maintained with the deal he and Skelos struck today. The IDC already has been tight with the Senate Republicans since they broke off from the main Democratic conference two years ago.

As for titles, always a consideration in Albany, Skelos and Klein will be called conference leaders of their two groups and will “administer joint and equal authority” over what bills are heading to the chamber floor for votes, have dual roles in negotiating the state budget with the Assembly and governor, and share in decisions about appointments of individuals made to state and local boards and commissions. The two also will share in leadership and committee assignment decisions. Also, Skelos and Klein will, in a unusual move for a legislative body, trade off on the title of temporary president – essentially, the head of the Senate – every two weeks.

The depth of the deal shows just how far the Republicans were willing to compromise in order to stay in power for another two years. The deal will all be put in writing in new Senate rules when the Legislature returns in January for its 2013 session.

Senate Democrats dismissed the term “coalition’’ to describe the deal.

“This is not a coalition, but a coup against all New Yorkers who voted for Democratic control of the Senate and a progressive state government,’’ said Mike Murphy, a spokesman for the main Senate Democratic conference headed by Sen. John Sampson of Brooklyn. He said the “real victims’’ of Tuesday’s deal will be New York’s residents.

The agreement was announced as lawyers for Democrats and Republicans were squaring off in a Montgomery County courtroom Tuesday over 877 contested paper ballots that have not yet been counted in a race between the GOP candidate, George Amedore, and Democrat Cecilia Tkaczyk in a district that stretches from the Mohawk Valley to the Mid-Hudson Valley near Poughkeepsie. While Amedore holds about a 100-vote lead, Democrats say their candidate would win if the court rules that all or most of the 877 remaining ballots had to be opened and counted.

That put pressure on Republicans to cut a deal with Klein and his fellow breakaway Democrats – Diane Savino of Staten Island, David Carlucci of Rockland County and David Valesky of Madison County – before the Amedore and Tkaczyk race was decided, most insiders agree.

Democrats have been arguing for weeks that the IDC members, as Democrats, should honor the votes of their constituents that put them in office and re-join the main Democratic conference. The IDC group was also, until Smith’s entrance on Tuesday, all white, and that created further tensions with accusations that the group, by joining with Republicans, would deny African-American and Latino Senate Democrats their place of power in Albany.

The power sharing deal may also help explain why the governor did not -- as he did two years ago when chaos loomed over which party might control the Senate – ask the state’s top judge to get involved and expedite the court case in the 46th Senate court fight.

While the power-sharing agreement took up a mere one paragraph in a joint statement released by Skelos and Klein, it remains to be seen how the process will work. Officials said there is no more of a deal, in writing, and that final provisions will be made public when the new Senate rules are adopted in January. To make it all legal, negotiators said the title switch every two weeks between Skelos and Klein as Temporary President keeps intact an actual provision in the state constitution that such a position be filled in the Senate.

Unclear is what other side deals were made, such as leadership posts that pay additional stipends beyond the base pay of $79,500, choice committee chair assignments that can give additional access to prestige and campaign donations for their holders, and things like state-issued cars and office locations.

And there are numerous policy question marks.

Will the Republicans let out onto the floor, for instance, a measure pushed by Staten Island’s Savino to legalize medical marijuana when, typically, such bills have been kept from ever reaching a floor vote as a way to kill unpopular items with GOP senators?

And will the breakaway Democrats – called renegades by those in the main Democratic conference – join with the regular body of Democrats to push, over the opposition of the GOP, stalled efforts to raise the state’s minimum wage?

Skelos and Klein declined interview requests Tuesday, preferring to let their brief statement stand for the day.

A spokesman for Cuomo, who sources said earlier in the day Tuesday was pleased with word of a looming deal, did not immediately return calls for comment.

The power fight has its consequences. Democrats have insisted what they call progressive legislation – such as new gun control restrictions and public financing of campaigns – will not happen now that the Republicans remain partially in control.

For Cuomo, it means he will have in power a group he has already cut major deals with during his first two years in office, including a property tax cap, a public school teacher evaluation system and higher pension contributions by new government workers. Republicans say it also means he will not have to re-wage certain fights, such as some efforts to weaken the property tax law’s components, again with a Democratic-run Assembly and Senate.

For upstate, the fight was important.

The GOP base of support is upstate and Long Island, while most Democrats in the Senate hail from New York City. Republicans say that would have played out by upstate being ignored if the Democrats took control. Democrats have called that claim absurd, and say the GOP has been in charge of the Senate during decades of economic decline upstate.

“It preserves the balance in the state,’’ Sen. George Maziarz, a Newfane Republican, said of the New York City-led Assembly and the upstate and suburban New York-led Senate.

“This is one state, and we either sink or swim together,’’ Sen. Liz Krueger, a Manhattan Democrat, said in a recent interview. “Republicans are constantly making arguments that it’s the city versus everyone else, and nothing else can be further from the truth when you look at where the revenues come from and go to in the state of New York.’’

For Cuomo, he gets to start a second term without having to pay attention to the demands of the main Senate Democratic conference, a group he did not help in their bid to re-take the Senate and whose members include those who have questioned his Democratic Party credentials because of the help he gave GOP candidates, such as the warm words he heaped upon freshman Sen. Mark Grisanti, a Buffalo Republican, in his re-election bid.

In the IDC, Cuomo gets a small group of Democrats who have already shown a willingness to cut deals with Senate Republicans and, as importantly, given him the public respect he is known to demand in fellow politicians.

“I think Governor Cuomo has singlehandedly brought back the Democratic brand in New York,’’ Klein said in a recent interview. “He has shown that we can govern in a bipartisan fashion and has singlehandedly shown the Legislature can function again. His legislative agenda is really my legislative agenda.”