Whitbread had intended to spin off the chain as a separate firm, but said a straight sale was more profitable.
Chief executive Alison Brittain said Whitbread would now focus on its Premier Inn business in the UK and Germany.
Whitbread bought Costa, which is now the UK’s biggest coffee chain, for just £19m in 1995.
At the time, it had just 39 outlets. It now has more than 2,400 UK coffee shops, as well as some 1,400 outlets in 31 overseas markets. Costa Express has 8,237 vending machines worldwide.
Whitbread shares rose more than 17% in early Friday trading.
Analysis: Dominic O’Connell, Today business presenter
For years, demerger was a dirty word at Whitbread. When asked, as they often were, about the logic of having a hotel chain and a coffee chain in the same company, executives would extol the benefits of having two leisure brands under one roof.
Shareholders were always less convinced, but were happy to go along with the idea while Whitbread grew its revenue, profits and share price at a steady clip over the last decade.
All that changed with the arrival of Alison Brittain as chief executive – and the appearance on the shareholder register of Elliott Management, an aggressive, deep-pocketed US hedge fund with a track record of shaking up big companies. It pushed hard for a demerger, and Ms Brittain, who judged that Costa and Premier had reached sufficient scale to stand on their own feet, opened the door.
The plan was that Costa would be spun off at some time in the next two years, but Coca-Cola pre-empted that with a knockout offer.
While this is a landmark deal for Whitbread, it is also a significant move for Coca-Cola, taking it into hot beverages for the first time and, it hopes, providing the growth for which its investors have been crying out.
Speaking on the BBC’s Today programme, Ms Brittain explained that Coca-Cola wanted to buy Costa because “they want the coffee product, they have no coffee in their range”.
She said the money from the sale would be used to expand the Premier Inn chain, return some cash to shareholders, pay down debt and boost the pension fund.
Whitbread announced earlier this year that it was planning to spin off Costa.
Whitbread had been under pressure to consider a break-up of the business after hedge fund Elliott Advisors became the company’s largest shareholder.
Elliott reportedly put forward a demerger plan after building up a 6% stake in the firm.
As well as being the largest UK coffee chain, Costa is also the world’s second largest. It is looking to triple its presence in China, where it is second to Starbucks.
From beer to beds: Whitbread’s long history
1742 – Samuel Whitbread forms partnership with Godfrey and Thomas Shewell
1750 – Whitbread creates UK’s first mass-production brewery in London
1868 – Begins producing beer in bottles
1968 – Starts brewing Heineken under licence
1974 – Opens first Beefeater steakhouse
1990s – Buys David Lloyd Leisure, Marriott Hotels, TGI Fridays, Pizza Hut, Premier Lodge
1995 – Buys Costa Coffee from Sergio and Bruno Costa for £19m when it had 39 outlets
2000 – Sells brewing business to focus on Premier Inn and Costa
Nicholas Hyett, equity analyst at Hargreaves Lansdown, described the deal as “a bitter-sweet moment for Whitbread investors”.
“On the one hand, £3.9bn is an undeniably rich valuation and likely far better than Costa could achieve as an independently listed company, valuing its earnings higher than those of the mighty Starbucks,” he said.
“On the other, Costa has long been the jewel in Whitbread’s crown and some will be sad to see it go at any price, especially given the growth potential in China and elsewhere.”
Whitbread also owns restaurants Beefeater and Brewers Fayre. Over the years, it has owned well-known brands such as TGI Fridays, Pizza Hut and Marriott Hotels.
The deal is subject to the agreement of Whitbread’s shareholders and various other approvals, including from anti-trust regulators.
It is expected to complete in the first half of next year.