Robert F. Bukaty/Associated Press

Pushed by a bankruptcy judge eager to save thousands of jobs, Hostess Brands and one of its biggest unions agreed to mediation on Monday, in a last-ditch effort to avoid winding down Hostess, the bankrupt maker of Twinkies and Wonder Bread.

At the behest of the judge, Hostess Brands and the Bakery, Confectionery, Tobacco Workers and Grain Millers Union, which represents 5,600 Hostess workers, will meet with a mediator on Tuesday to try to narrow their differences toward a labor agreement.

If the mediation succeeds, it could prevent the liquidation of the company and save 18,500 jobs. Otherwise, Hostess is likely to auction off its well-known brands, leaving the fates of those workers in limbo.

In January, Hostess, an 82-year-old company, filed for Chapter 11, just three years after emerging from bankruptcy. At the time, the company said it was unable to pay its debts and needed to make deep cuts in labor costs to survive.

Hostess was able to reach a new contract with the Teamsters, its largest union. But talks between the company and the bakery workers deadlocked, and the union went on strike on Nov. 9. With production slowing and its finances dwindling, the company announced plans on Friday to liquidate.

Judge Robert D. Drain of the Federal Bankruptcy Court for the Southern District of New York pushed hard for the two sides to try one last round of talks. The judge expressed worry that neither side had exhausted all efforts to avoid liquidation. He especially urged the bakery union to seek mediation, suggesting that it might face significant legal claims if Hostess is forced to liquidate.

“I’m giving the union, as well as the debtor and their lenders, a chance to work out their issues in private,” Judge Drain said. “If they don’t take it, it’s not that the issues won’t be worked out. They will, but it will be done in public and in an expensive way.”

The unions have been at the center of the struggle for Hostess’s survival.

When Hostess filed for bankruptcy, the company insisted that its labor costs and union rules were unsustainable, and it moved to renegotiate the contracts. One work rule required that two separate trucks be used to ship bread and cake products to a single retailer. The company also indicated that it faced $52 million in workers’ compensation claims.

The two main unions, the bakery workers and Teamsters, countered that years of mismanagement were to blame. The private equity backers had loaded the company with debt, the unions said, making it difficult to modernize Hostess’s bakeries or product offerings. The bakery workers’ president, Frank Hurt, called the private equity owners “vulture capitalists.”

The two unions took different approaches to the labor negotiations.

The Teamsters hired a financial consultant, Harry J. Wilson, who had worked on the General Motors restructuring. He laid out just how much the union could get and still allow for a recovery of the company.

“We’re pragmatic when it comes to this situation,” said Ken Hall, the Teamsters’ secretary-treasurer. “We know that it’s a tough situation. We knew that because of mismanagement, the company was in a real hole.”

After eight months, the Teamsters eventually reached an agreement with the company. The union agreed to a contract that cut pay by 8 percent immediately — with that cut shrinking to 5 percent next year. The Teamster workers, most of whom drive trucks for Hostess, average about $20 an hour; the bakery union workers, $16 an hour.

The Teamsters contract reduced the company’s health contributions by 17 percent and suspended its pension contributions until 2015. The company had originally insisted on freezing the pension plan permanently and ceasing all contributions.

The Teamsters insisted on numerous concessions from management. The company eventually agreed to give Hostess’s unions two seats on its board, a 25 percent share of company stock and a $100 million claim in bankruptcy. Last March, the Teamsters helped push out Hostess’s former chief executive after the board proposed tripling his salary even as he was demanding steep concessions from the workers.

In September, the Teamsters members voted narrowly to approve the deal, 53 percent to 47 percent.

The bakery workers union took a far more adversarial stance.

After Hostess’s unions had agreed to more than $100 million in annual cost concessions during Hostess’s previous bankruptcy, the bakery union thought it made little sense to agree to further cuts. It feared a deal would pull down wages and benefits throughout the industry, without saving Hostess.

“Our consultant said the debt load on the company was too heavy, and that we would be back in bankruptcy and facing liquidation in 12 to 16 months from now, even if we took more concessions,” David B. Durkee, the union’s secretary-treasurer, said.

The bakery union often derided Hostess’s management, saying it was composed of Wall Street investors and “third-tier managers” from nonbaking companies. It said the investors were trying to “resolve the mess by attacking the company’s most valuable asset — its workers.”

Both sides refused to budge, and the bakery union went on strike at 24 of the company’s 33 bakeries.

With the threat of liquidation looming, the Teamsters, in an unusual move, called on the bakery workers to hold a vote to determine whether the rank-and-file workers wanted to end their strike and accept Hostess’s offer — or face layoffs. Last Thursday, Mr. Hall of the Teamsters told the bakery workers that Teamsters members could not believe liquidation and layoffs were what the bakery workers “ultimately wanted to accomplish when they went out on strike.”

But the bakery union declined to hold such a vote. The next day, the company announced plans to liquidate and sell off its assets.

“There was this whole theory that the company was bluffing about liquidation and there was some white knight that was going to come and buy the company” Mr. Hall said.

Now, Hostess and the bakery union will meet at the offices of the company’s lawyers. Representatives for the Teamsters and the company’s bankruptcy lenders were also invited to attend. If the two sides cannot agree, Hostess’s lawyers are expected to appear in court on Wednesday morning to seek approval of their liquidation plan.

Potential suitors are already lining up.

Some of Hostess’s rivals may pursue deals for the company’s most popular brands, especially Twinkies. One possible buyer, Flowers Foods, disclosed on Monday that recent revisions to its bank lending agreements would allow the bakery company to borrow up to $700 million. That additional cash may help finance a deal, analysts said. A spokesman for Flowers did not return a call for comment.

Industry analysts say Grupo Bimbo, the world’s largest bread-baking company, is also in the mix. Bimbo, which already owns parts of Entenmann’s, Thomas’ English muffins and Sara Lee bakery, made — and then withdrew — a $540 million bid for Hostess during its previous bankruptcy. The company might just buy pieces, because it could face antitrust issues.

Financial investors like Sun Capital Partners and Metropoulos & Company have also said that they are interested in pursuing a deal for Hostess. Sun’s co-chief executive, Marc Leder, told Fortune, “I think that we could offer a slightly better, more labor-friendly deal than what was on the table last week.”