According to a report from Bloomberg, Microsoft and Nokia have been talking about this deal since Mobile World Congress in February. The deal was apparently very emotional for the Nokia board, who took a long time and met a reported 50 times before coming to an agreement. The main points were agreed to by mid-summer, and the talks went into overdrive in August. That one month was when the majority of the work was done to get this deal through.
From Microsoft's side, Steve Ballmer had decided that the company needed to have its own branded devices to succeed (what isn't said in the article is that Microsoft's in-house efforts with the Surface apparently weren't enough.) On the Nokia side of things, the plan to sell the mobile device unit was described as "emotional" and "soul-searching", and the subtext there seems to indicate that Nokia knew that something major had to be done, which could point to financial troubles for the company which has been losing money the last couple years.
Ultimately, the deal has been done, but a surprise revelation from the article is that Microsoft may still be "eyeing" BlackBerry. BlackBerry has been rumored to be up for sale for quite a while, and recently the company finally admitted as much. There have been plenty of rumors that put Samsung or Apple as names that may purchase BlackBerry, but now we have to add Microsoft to that list.