Publisher Rupert Murdoch said yesterday he has agreed to buy Denver oilman Marvin Davis' 50 percent stake in Twentieth Century-Fox Film Corp. for $325 million and valuable real estate.

The deal gives Murdoch control of a Hollywood studio that can provide programming for six independent television stations he is acquiring from Metromedia Corp.

Sources said yesterday that Murdoch and Fox Chairman Barry Diller plan to use the Fox studio and its library, containing approximately 1,000 films, to provide programming for Washington's WTTG-TV, Channel 5, and Metromedia stations in New York, Los Angeles, Chicago, Houston and Dallas. Murdoch agreed to acquire the Metromedia stations last May for $1.55 billion, but that transaction cannot be completed until it is approved by the Federal Communications Commission.

Murdoch purchased 50 percent of Fox from Davis last March for $250 million. He has been rumored to be interested in buying the rest of Fox since Davis decided not to be his partner in the Metromedia deal.

Davis and commodities trader Marc Rich acquired Fox four years ago for $722 million. Davis bought out Rich, a fugitive in a U.S. tax-evasion case, for $116 million last year.

In addition to the $325 million Davis will receive from Murdoch, the wealthy Denver businessman also will receive valuable Fox real estate in California and Colorado, including a 2.7-acre parcel on Fox's lot in Century City, Calif. The total value of the transaction is about $500 million, one source said.

As part of the transaction, Davis will transfer his interest in 54 acres of Fox real estate to Murdoch. Davis and Murdoch will continue as partners in construction of a Century City office building.

John Davis, son of the Denver oilman, who was a Fox executive, will leave the company for a new film venture, sources said yesterday.

"I am very pleased about the transaction," Fox Chairman Barry Diller said yesterday. "I think it is quite healthy."

Fox, which had net losses of about $89.7 million last year and $80.1 million in the first nine months of this fiscal year, will show a fourth-quarter operating profit, sources said, largely because of success with its hit film "Cocoon," which has grossed more than $70 million this year.

"Our Fox investment has been an extremely successful one," Davis said yesterday. "While it was not our intention to sell the balance of our interest at this time, we concluded that it made good business sense to accept Rupert Murdoch's offer.

"We will now concentrate on the development of our other activities, including the former Fox properties. And we will take advantage of our extremely strong and flexible position to actively explore other business opportunities."

Despite the statement yesterday that his position is "strong and flexible," there have been rumors recently that Davis is under some pressure to convert some of his holdings to cash. However, one source said that since Davis had recouped most of his investment in Fox prior to yesterday's announcement, he will make several hundred million dollars on the Fox buyout.

Murdoch, who recently gave up his Australian citizenship to become an American citizen, already owns The New York Post, the Chicago Sun-Times, The Boston Herald, The San Antonio Express and New York magazine in the United States. He would not have been able to receive FCC approval to purchase the Metromedia stations unless he became an American citizen.