The deal, for 800p a share, was finally agreed after three previous advances had been rebuffed. It is expected the deal will be completed in days.
The takeover looked like it might not happen at one point, after global financial turmoil sent shivers through the markets.
It will give the Dutch and Danish firms control of the UK's largest brewer, which is behind brands such as Newcastle Brown Ale, John Smith's and Foster's.
It is likely to lead to job losses at S&N, which employs around 3,300 staff in the UK at breweries in Manchester, Reading, Dunston and Tadcaster.
Heineken said it would create cost savings of £120m annually. Carlsberg will take full ownership of Russian joint venture BBH, which produces Baltika beer.
S&N's French, Greek, Chinese and Vietnamese operations will be transferred to Carlsberg.
Heineken will continue to hold the remaining businesses, principally the UK and Ireland, Portuguese, Finnish, Belgian, US and Indian operations. The businesses will be separated as soon as possible.
Carlsberg said the deal would further increase its exposure to attractive growth markets and enhance its position in Western Europe, as well as help it expand its Asian business platform in the Chinese and Vietnamese markets.
For Heineken, the acquisition will boost its plans for growth by giving it new distribution outlets in the UK and other markets to drive premium Heineken brand growth. In addition to gaining control of complementary brands with international appeal and potential.
Heineken will also be able to access to the fast growing UK cider market via Strong Bow, which is growing at 18.6%.
Jorgen Buhl Rasmussen, president and CEO of Carlsberg, said: "This is a truly transformational transaction for Carlsberg. In a single step we have created the world's fastest growing global brewer.
"We now have full control of our destiny in Russia and other BBH territories and I am truly excited about the new opportunities this will present to us."
Jean-Francois van Boxmeer, chairman and CEO of Heineken, said: "This is a significant strategic step for Heineken.
"It gives us undisputed leadership in Europe and creates significant opportunities in profitable markets to grow the premium Heineken brand.
"Our proven ability to create value from mature markets coupled with the step-change in revenue growth will drive our future expansion.
"I look forward to welcoming the Scottish & Newcastle employees into our business and learning from their unique experience and skills."
Sir Brian Stewart, chairman of S&N, said: "The management and employees of S&N have built a group with strong brands enjoying leading positions in both mature markets in Western Europe and growing emerging markets.
"The S&N board believes that the consortium's offer delivers a fair value for S&N, reflecting its growth prospects, and will be recommending that shareholders accept."
The deal could have implications for S&N and Heineken's advertising agencies. TBWA\London currently handles the John Smith's account and M&C Saatchi has Foster's and Kronenbourg.
Starcom holds the UK media planning and buying account for S&N, while MindShare holds the global account for Heineken.
S&N also has a number of below-the-line agencies on its roster, including WWAV Rapp Collins, EHS Brann and Story.