WP's problem was in the length of time between flagship phones, and the length of time between updates.
WP7 came out, with a big marketing push, and it was an overall good OS. The initial wave of phones were received OK, but the platform had some warts that needed to be cleaned up.
What MS did is announce that they were going to release an update, and then do a huge rewrite. 7.5 came out, fixed all the obvious show stopper issues, and then all MS needed to do was keep iterating the platform.
But they didn't. They waited 2 years to release 8.0 instead. Which regressed features from 7.5, asked developers to learn a new API, and had features missing from that API that used to exist in 7.5.
At this point the platform has lost a lot of mind share, new phones need to come out yearly, not on some random scattershot schedule. iOS sees regular platform updates, and is iterating much faster. Android is still a dumpster fire at this point, and could have been eclipsed.
So then WP8 finally comes out. Android is in a much better spot, and iOS looks pretty damn spiffy.
New API, huge chunks of functionality are missing, making many types of apps not even possible to write.
But hey, the Lumia 1020 comes out and gains a TON of attention. The best mobile camera ever made!
And then Microsoft up and does nothing for another two years. They stop releasing flagship handsets, and basically let the brand die.
Finally 8.1 comes out, but by this time it is too late. No developers, the APIs finally aren't terri-bad but they are still miserable to develop for compared to anything else, and all mind share has been lost. 8.1 is actually a good OS (unless you are a developer), but 7.5 was also a good OS, 4 years earlier.
If MS had released updates to the 7.x line every 6 months, focusing on making the developer's lives easier, they could have won. 7.x required far fewer resources than Android, ran much smoother, and performed well on lower cost devices.
But instead someone in the MS engineering department won, and Windows Phone underwent a complete OS rewrite. The reason was merging the Mobile org and the Windows org together, and Engineers played politics instead of writing software, thus one of the kernel teams had to go.