NEW YORK — Halliburton is buying rival oilfield services company Baker Hughes Inc. in a cash-and-stock deal worth $34.6 billion.

The deal comes just days after talks between the two had stalled and Halliburton prepared to go hostile with its takeover bid. The combined energy company would generate slightly larger revenue than Schlumberger Ltd., currently the world’s biggest oil services company.

Companies like Halliburton and Baker Hughes can be the first in the energy sector to feel pressure from plunging prices, and both have seen their stock fall in recent months. Global oil prices have tumbled 31 percent over the past five months to levels not seen in four years.

The transaction is valued at $78.62 per Baker Hughes share, a 31 percent premium to its Friday closing price of $59.89. Baker Hughes shareholders will receive 1.12 Halliburton shares plus $19 in cash for each share they own.

The companies put the deal’s enterprise value at about $38 billion.

When the transaction is complete, Baker Hughes stockholders will own approximately 36 percent of the combined company.

On Friday, it appeared that talks were falling apart, with Baker Hughes saying Halliburton had refused to raise its first

and only offer. The company did not say what that offer was.

Halliburton Chairman and Chief Executive Officer Dave Lesar said Monday that the combined company will realize annual cost savings of nearly $2 billion.

Halliburton plans to finance the cash portion of the acquisition through a combination of available cash and debt.

The boards of both Houston companies approved the deal unanimously, and it’s targeted to close in the second half of 2015. Shareholders and regulators must still sign off on the tie-up.

Halliburton said it is willing to divest businesses that generate up to $7.5 billion in revenues if required to by regulators. But the company believes that significantly less divestitures will be needed.

Halliburton also has agreed to pay a termination fee of $3.5 billion if the transaction ends due to a failure to obtain the necessary antitrust approvals.

Baker Hughes’ stock rose more than 15 percent in Monday premarket trading, while shares of Halliburton fell more than 3 percent.