TOKYO (Reuters) - Japan's Toshiba Corp 6502.T agreed on Wednesday to sell its prized semiconductor business to a group led by U.S. private equity firm Bain Capital LP, a key step in keeping the struggling Japanese conglomerate listed on the Tokyo exchange.

In a last-minute twist to a long and highly contentious auction, Toshiba said in a late-night announcement through the exchange it agreed to sign a contract for the deal worth about 2 trillion yen ($18 billion).

The decision to sell the world’s No. 2 producer of NAND memory chips, first reported by Reuters, was made at a board meeting earlier on Wednesday.

Late on Tuesday, sources had said Toshiba was leaning towards selling the business to its U.S. joint venture partner Western Digital Corp WDC.O.

It’s unclear whether the sale to the Bain Capital-led group will proceed smoothly, as Western Digital has previously initiated legal action against Toshiba, arguing that no deal can be done without its consent due to its position as Toshiba’s joint venture chip partner.

Toshiba said the agreement assumes the deal would weather legal challenges raised by Western Digital.

A Western Digital spokeswoman said the company did not have an immediate comment.

Toshiba said the sale would boost its finances by 740 billion yen after taxes. That would pull it out of negative shareholder equity, a key step it aims to achieve by March to remain listed.

Bain Capital has partnered with South Korea's SK Hynix Inc 000660.KS and brought in U.S. buyers of Toshiba chips such as Apple Inc AAPL.O and Dell Inc [DI.UL] to bolster its bid. Memory product maker Kingston Technology and data storage firm Seagate Technology Plc STX.O are also part of the group.

The Toshiba announcement referred to a group with foreign companies, but did not cite SK Hynix. Rather, it said two Japanese state-backed investors - the Innovation Network Corp of Japan (INCJ) and the Development Bank of Japan (DBJ) - were considering a future capital tie-up. Bain had previously invited the two into the group, sources have said.

A DBJ spokesman declined to comment. The INCJ could not immediately be reached for comment.

The logo of Toshiba Corp is seen behind trees at its headquarters in Tokyo, Japan October 1, 2015. REUTERS/Toru Hanai/File Photo

The make-up of the consortium could spell trouble ahead, said Hideki Yasuda, an analyst at Ace Research Institute.

“The large number of stakeholders could complicate decision-making and slow down key investment decisions,” he said, adding the participation of Toshiba clients would also sap the ability of the chips business to negotiate competitively on pricing.

Indeed, Toshiba announced the deal more than nine hours after Reuters reported the deal had been reached because, one source said, it was waiting for commitment letters from all the Bain consortium members.

Bain’s win has been hard fought as wrangling went down to the wire.

Late on Tuesday, the Western Digital-backed consortium, which includes KKR & Co LP KKR.N, appeared to be in the lead, but the California-based firm would not agree to limits to any future stake in the chip business that had been demanded by Toshiba, said one person briefed on the matter.

Sources declined to be identified as they were not authorised to speak about discussions on the sale.

SCRAMBLE FOR FUNDS

After a slew of revised bids and changing alliances among suitors, an agreement comes not a moment too soon for Toshiba.

It has been under pressure from its lenders to clinch a deal this month to ensure enough time for regulatory reviews so it can finish the sale by the end of the financial year in March.

Without a sale, Toshiba would not have the billions of dollars it needs to plug a hole in its finances caused by its now bankrupt U.S. nuclear unit Westinghouse, and could be delisted.

Also, the semiconductor business requires huge amounts of investment, and Toshiba's chip unit risks losing its competitive ability as rivals such as Samsung Electronics 005930.KS roll out big capital spending plans.

Western Digital has already taken its dispute with Toshiba to the International Court of Arbitration to prevent a sale without its consent, and a source with knowledge of the matter has said it is prepared to seek an immediate court injunction should the deal not go its way.

The Bain-led group had been chosen in June as preferred bidder, but those talks lapsed as Japanese government investors, who had been part of that consortium, told Toshiba they were reluctant to close a deal with the Western Digital legal challenges still pending.