Nokia, which will continue to make networking equipment and hold patents, was once the world's dominant handset maker but was long since overtaken by Apple and Samsung in the highly competitive market for more powerful smartphones.
Nokia's Canadian boss Stephen Elop, who ran Microsoft's business software division before jumping to Nokia in 2010, will return to the US firm as head of its mobile devices business — a Trojan horse, according to disgruntled Finnish media.
He is being discussed as a possible replacement for Microsoft's retiring CEO Steve Ballmer, who is trying to remake the US firm into a gadget and services company like Apple before he departs, though it has fallen short so far in its attempts to compete in mobile devices.
"It's very clear to me that rationally this is the right step going forward," Elop told reporters, though he added he also felt "a great deal of sadness" over the outcome.
In three years under Elop, Nokia saw its market share collapse and its share price shrivel.
In 2011, after writing a memo that said Nokia was falling behind and lacked the in-house technology to catch up, Elop made the controversial decision to use his former firm Microsoft's Windows Phone for smartphones, rather than Nokia's own software or Google's ubiquitous Android operating system.
Nokia, which had a 40 percent share of the handset market in 2007, now has a mere 15 percent share, with an even smaller 3 percent in smartphones.
Shares in Nokia surged 39 percent to 4.10 euros on Tuesday as investors who had borrowed and sold the stock to bet on further price falls rushed to buy back to limit their losses. They are still only a fraction of their 2000 peak of 65 euros.
After today's gains the whole company is worth about 15 billion euros, a far cry from its glory days, when it peaked at over 200 billion euros.
Microsoft shares in Frankfurt were down about 2.2 percent.
Sold for "peanuts"
The sale of the handset business is not the first dramatic turn in the 148-year history of a company that has sold everything from television sets to rubber boots, but it was taken as a hard blow in its native Finland.
For many Finns, the fact that a former Microsoft executive had come to Nokia, bet the firm's future on an alliance with Microsoft, laid off tens of thousands and then delivered it into Microsoft's hands, was a galling snub to national pride.
"(Elop's predecessor) Jorma Ollila brought a Trojan horse to Nokia," widely read tabloid Ilta-Sanoma declared in a column. Ollila built Nokia into a global powerhouse but was blamed for being late to recognize the threat of Apple's iPhone and the smartphone revolution.
"As a Finnish person, I cannot like this deal. It ends one chapter in this Nokia story," said Juha Varis, Danske Capital's senior portfolio manager, whose fund owns Nokia shares. "On the other hand, it was maybe the last opportunity to sell it."
Varis was one of many investors critical of Elop's decision to bet Nokia's future in smartphones on Microsoft's Windows Phone software, which was praised by tech reviewers but hasn't found the momentum to challenge the market leaders.
"So this is the outcome: the whole business for 5 billion euros. That's peanuts compared to its history," he said.
Alexander Stubb, Finland's Minister for European Affairs and Foreign Trade, said on his Twitter account: "For a lot of us Finns, including myself, Nokia phones are part of what we grew up with. Many first reactions to the deal will be emotional."
Nokia's new interim CEO Risto Siilasmaa painted a picture of just how grudgingly the call to sell had been arrived at, describing how the board had met almost 50 times after the approach by Microsoft as it explored alternatives to a sale.
Ballmer, at a news conference in the Finnish capital, sought to assuage fears the deal would hit jobs in the Nordic country and said Microsoft would build on the recent growth of Nokia's flagship Lumia smartphones.
Nokia said it expected around 32,000 people of its roughly 90,000 worldwide staff would transfer to Microsoft, including about 4,700 who will transfer in Finland.
It is also a pivotal moment for Microsoft, which still has huge revenues from its Windows computer operating system, Office suite of business software and the X-Box game console, but has failed so far to set up a profitable mobile device business.
Microsoft's own mobile gadget, the Surface tablet, has sold tepidly since it was launched last year.
"It's a bold step into the future — a win-win for employees, shareholders and consumers of both companies," Ballmer said in a statement. "Bringing these great teams together will accelerate Microsoft's share and profits in phones and strengthen the overall opportunities for both Microsoft and our partners across our entire family of devices and services."
The move leaves the Finnish company with Nokia Solutions and Networks, which competes with the likes of Ericsson and Huawei in telecoms equipment, as well as a navigation business and a broad portfolio of patents, which will be licensed to Microsoft.
The Nokia deal thrusts Microsoft deeper into the hotly contested mobile phone market, despite some investors urging it to stick to its core strengths of business software and services.
Elop will return to Microsoft as its board ponders a successor to Ballmer, who will depart in the next 12 months.
Activist fund manager ValueAct Capital Management, which has been offered a board seat, is among those concerned with Ballmer's leadership and his attempts to plough headlong into the lower-margin, highly competitive mobile devices arena.
Others applauded Ballmer's aggressive gambit.
"Microsoft cannot walk away from smartphones, and the hope that other vendors will support Windows Phone is fading fast. So buying Nokia comes at the right time," said Carolina Milanesi, an analyst at Gartner.
"In today's market it is clear that a vertical integration is the way forward for a company to succeed. How else could Microsoft achieve this?"
As part of Microsoft, Elop will head an expanded Devices unit. Julie Larson-Green, who in July was promoted to head a new Devices and Studios business in Ballmer's reorganization, will report to Elop when the deal is closed.
Analyst Tero Kuittinen at consultancy Alekstra said the sale price of Nokia's phone business, about a quarter of its sales last year, represented a "fire sale level", though others were less clear about what a shrunken Nokia was worth.
"What should be paid for a declining business, where market share has been constantly lost and profitability has been poor?" said Hannu Rauhala, analyst at Pohjola Bank. "It is difficult to say if it's cheap or expensive."
Nokia is still the world's No. 2 mobile phone maker behind Samsung, but it is not in the top five in the more lucrative and faster-growing smartphone market.
Sales of Nokia's Lumia series have helped the market share of Windows Phones in the global smartphone market climb to 3.3 percent, according to consultancy Gartner, overtaking ailing BlackBerry Ltd for the first time this year. Still, Google Inc's Android and Apple's iOS system make up 90 percent of the market.
Nokia said in a statement it expected that, apart from Elop, senior executives Jo Harlow, Juha Putkiranta, Timo Toikkanen, and Chris Weber would transfer to Microsoft when the deal is concluded. It did not say what roles they would take there.
The deal is expected to close in the first quarter of 2014, subject to approval by Nokia shareholders and regulators.