Nokia will also license its patents and mapping services to Microsoft. Nokia shares jumped 45% on news of the deal.
The purchase is set to be completed in early 2014, when about 32,000 Nokia employees will transfer to Microsoft.
While Nokia has struggled against competition from Samsung and Apple, Microsoft has been criticized for being slow into the mobile market.
“It’s a bold step into the future – a win-win for employees, shareholders and consumers of both companies,” Steve Ballmer, chief executive of Microsoft, said in a statement.
The transaction is subject to approval by Nokia shareholders and regulators.
Microsoft, one of the biggest names in the technology sector, has struggled as consumers have shunned traditional PCs and laptops in favor of smartphones and tablet PCs.
Critics say the firm has been too slow to respond to the booming market for mobile devices. It launched its Surface tablet PCs last year, but sales of the devices have been relatively slow.
Analysts said that the company wanted to make sure that it got its strategy right in the mobile phone market.
Nokia was once a leader in mobile phones, but the firm’s sales fell 24% in the three months to the end of June from a year earlier.
It sold 53.7 million mobile phones during the quarter, down 27% on last year.
However, sales of its new Lumia phones, which run a Microsoft operating system, rose during the period.
Microsoft has also agreed a 10-year licensing arrangement with Nokia to use the Nokia brand on current mobile phone products.
Nokia also announced changes to its leadership team as a result of the sale.
Stephen Elop will step down as president and chief executive of Nokia Corporation and resign from the company’s board.
The firm said that he would become the executive vice president of the Devices & Services unit, adding that it expected him to “transfer to Microsoft at the anticipated closing” of the deal.
The transfer of Stephen Elop to Microsoft comes at a time when the company is looking for a new chief executive.
The current head of Microsoft, Steve Ballmer, announced last week that he would be retiring and is expected to leave the company within the next 12 months.
Stephen Elop who left Microsoft to join Nokia in 2010, has been cited by some as one of the frontrunners to replace Steve Ballmer.
Nokia said that once the sale is completed, it will concentrate on three key businesses – network equipment manufacturing, mapping and location services, and the development and licensing of technology.
Earlier this year, it agreed to buy Siemens’ 50% stake in their joint venture, Nokia Siemens Networks (NSN), which makes telecoms network equipment, for 1.7 billion euros.