United Airlines (search) and its machinists' union announced an agreement in principle on a contract Tuesday, narrowly heading off the need for a bankruptcy judge's ruling that could have triggered a strike.

A formal contract agreement is to be presented in federal bankruptcy court June 17. Judge Eugene Wedoff gave the two sides until then to work out remaining details.

The agreement in principle came just hours after United's mechanics ratified a five-year contract, giving the nation's No. 2 airline a pair of crucial labor victories that it needed to avoid an employee walkout and strengthen its bid to come out of bankruptcy.

The tentative deal with the International Association of Machinists and Aerospace Workers (search), representing 20,000 baggage handlers, customer service representatives and other ground workers, was disclosed in court with Wedoff poised to rule on United's request to break the contract. The IAM had warned it would shut down the carrier with a walkout if that happened.

The two sides bargained through much of the holiday weekend and worked out the basic terms of an agreement in negotiations at an undisclosed location near O'Hare International Airport (search) shortly before the afternoon court session.

The company asked for $175 million in yearly concessions from the IAM, its largest union, to complete a companywide package of $700 million in annual labor savings. When complete, the tentative pact must still be ratified by the IAM's membership.

The mechanics agreed by a 59 percent to 41 percent vote to ratify a contract containing $96 million in annual concessions, including 3.9 percent pay cuts and reduced benefits.

The 7,000 mechanics, like other United employee groups, have expressed anger about a second round of reductions during the airline's 2 1/2-year-old bankruptcy but knew they ran the risk of putting United out of business with a vote rejection and strike. About 81 percent of members voted, Aircraft Mechanics Fraternal Association (search) spokesman Richard Turk said.

Like the machinists, the mechanics' union had authorized a strike if the vote had failed, and its members voted down a previous contract agreement in January. But the union's leadership told them a "No" vote this time could produce worse terms than the ones negotiated.

"This vote signifies ratification but not acceptance," said AMFA Local 9 president Joseph Prisco, whose unit represents 52 percent of United mechanics — based in San Francisco and Hawaii.

The mechanics' new pay cuts, effective immediately, will come on top of 14 percent reductions made two years ago. Reduced benefits, such as sick days and holidays, would account for the rest of the labor savings.

A 5 percent defined-contribution pension plan is part of the new contract, replacing the former defined-benefits plan. The mechanics' union also will get $40 million in convertible notes upon United's exit from Chapter 11 bankruptcy.

Interim contracts for both the machinists and the mechanics were to expire Tuesday.

Separately, United said it has to return four of its Boeing 767s to leasing companies because of an inability to reach a deal on new rental terms. Four other disputed aircraft also are at risk and might have to be returned if ongoing lease negotiations fail, United spokeswoman Jean Medina said.

The repossession is the result of a court ruling in early May by the U.S. 7th Circuit Court of Appeals, reversing a November order by Wedoff that barred lessors from taking back 14 of United's 460 airplanes. The court said the planes could be repossessed unless United paid off its rental payments in full.

United subsequently rejected leases on six of the disputed planes, leaving eight vulnerable to repossession.

Medina said one route will be dropped because of the repossessions, between Chicago and Buenos Aires. United will continue to fly to Buenos Aires out of Washington.