The government of Costa Rica has bought the banana plantations on its west coast from United Brands Co. and will convert them to growing cacao beans (used to make chocolate) as a result of an agreement signed last week.

The pact, the result of talks that began in mid-1983 with the assistance of the U.S. Embassy, transfers 4,200 acres and 500 buildings to Costa Rica for 60 million Costa Rican colones, or about $1.24 million.

United Brands, formerly United Fruit, also is turning over free a wharf and a railroad with eight locomotives and about 200 freight cars, and will sell its 3,000 employes their homesteads and pay them about $5 million in severance, according to George M. Skelly, United Brands senior vice president and general counsel.

"It's all a gesture on our part," Skelly said. "We just could not continue putting money in the banana business there. It just wasn't economical."

The Costa Rican government, which feared unemployment riots and an economic crisis from a sudden departure by United Brands, expressed satisfaction just short of delight with the agreement.

"It's a solution that is satisfactory for us and for them and especially for the landless workers . . . ," Ambassador Fernando Zumbado said. "Cacao will work out very well and permit even a little industrialization. The support of the U.S. government was a determining factor."

Also pleased, and relieved, is the State Department, which was caught during the talks between its traditional defense of U.S. corporations abroad and the Reagan administration's desire to bolster Central America's premier democracy.

"If United Brands is happy and Costa Rica signed it, we're happy, believe me," a State Department official said.

In effect, the pact takes United Brands out of the banana-growing business it started in Costa Rica at the turn of the century. Its banana and palm oil operations on Costa Rica's east coast will not be affected, but these now involve buying and shipping only independently grown fruit.

The west coast plantations included schools, hospitals, stores and housing for 3,000 employes; 47,000 other Costa Ricans earned a living directly or indirectly from them, according to Costa Rican government estimates. The company also paid an estimated $40 million a year to the government in concessionary payments and taxes on exports and workers' incomes.

The breakthrough came when United Brands lowered its demand from $15 million in U.S. dollars to about a tenth of that sum in Costa Rican currency, Skelly said.

"They probably would have paid to get out of there," the State Department official said.