CVS Caremark Corp and Walgreen Co ended a fight over reimbursements for drug prescriptions, saving a relationship worth billions of dollars and lifting shares of the two biggest U.S. drugstore chains.

The companies said in a statement on Friday that Caremark members would continue to be able to have their prescriptions filled at Walgreens pharmacies but did not disclose the exact terms of the new agreement. They did say it was a multi-year deal.

Walgreen shares were up 8.4 percent at $31.74 in premarket trading, while CVS rose 5.2 percent to $33.50.

Analysts had said the companies' dispute was jeopardizing billions of dollars in sales for each chain, and predicted they would quickly come to a resolution, despite a publicly acrimonious conflict.

Top executives at both companies in a joint statement recognized how high the stakes were.

The agreement makes good business sense, said Walgreens Executive Vice President of Pharmacy Kermit Crawford.

His counterpart at CVS, Per Lofberg, president of CVS Caremark's PBM business, said the new contract eliminates any current or long-term disruption for our clients.

Walgreen, the No. 1 U.S. drugstore chain, said early last week that it would phase out its arrangement to fill prescriptions for millions of CVS Caremark drug plan members. It cited CVS's efforts to divert customers to its own pharmacies and inadequate reimbursement rates.

CVS retaliated two days later by saying it would dump Walgreen from its network by July 9.

CVS's pharmacy benefits management business administers prescription drug benefits for employers and health plans, as well as operating a large mail-order pharmacy.

Walgreen's arrangement with CVS accounts for about 7 percent of sales, but analysts said Walgreen was willing to risk severing its relationship with Caremark to send a larger message out to other PBMs that it would fight a trend to shrink reimbursements.

Walgreen holds great sway in the PBM business in part because it fills about one in five U.S. drug prescriptions.

Walgreens operates about 7,500 stores out of the 64,000 pharmacies served by Caremark's network.

(Reporting by Phil Wahba; Editing by Lisa Von Ahn)