This problem existed for me with Yosemite but only happened occasionally, but now on El Capitan it happens almost every time I wake the computer from sleep. I've been trying to debug this issue (on and off) for months (years?). None of the proposed solutions on this thread or any other thread made any difference at all. But just last week I discovered a partial solution! Sorry for the longish explanation, I wanted to try and be as clear as possible.
My scenario (similar to OP):
- At work - MBP Retina connected to external LG IPS by thunderbolt, no mirroring and extended desktop, laptop set as primary display
- At home - Connected to external Dell 4K by thunderbolt, laptop in clamshell mode (ie laptop closed)
At work, no issues, ever - windows always left where I had them originally, and no difference whether it was screensaver/sleep/suspend. So let's assume "work mode" is fine. At home, however, if the computer went to screensaver or sleep mode, I'd wake it up to discover any number of random windows off the desktop/offscreen, with only 1/4" visible. Windows would move off both left and right sides too, without any indicator or pattern as to which would be preferred.
But I realised last week that OSX remembers not just screen settings, but it also seems to remember desktop settings for different external screens (and, for me, different model/brand screens - I assume this is a feature, I've not checked/confirmed).
So, on that theory, I decided to see if I could set up the home situation with both screens active initially, to try and induce different behaviour after screensave/sleep. At work, mirroring is off. You can't change mirroring when in clamshell mode, so for this test at home I had both screens initially on. The first important step was to make the external Dell the primary monitor by moving the dock over. Second step was to turn on mirroring. Then close the laptop lid and go into clamshell mode.
End result - for over a week, not a single window has moved off screen coming out of screensaver or sleep. The problem is not completely solved - some windows still move occasionally, but they do so WITHIN THE BOUNDARIES of the external Dell monitor so they are much easier to reposition. Changing these settings for the Dell (at home) has not had any impact at all on the work (non-mirrored) settings for the LG IPS.
Side note, changing the settings in order seems to be important - when I tried setting mirror mode first but didn't change the primary monitor (which you can't do once mirroring is turned on), then the random offscreen window issue would still continue.
Conclusion - something about mirroring isn't handling clamshell mode correctly, and partially ignoring primary monitor settings in some way. It seems to be a sequence/order issue. The partial solution is more than enough for me - dragging 10+ windows using only 1/4" of title bar to grab was seriously annoying every time I woke the machine up. Now I only have to reposition perhaps one or two windows that have moved a little, but no part of any window now moves offscreen at all. It's like the sequence I configured the settings in was important for setting the display boundaries, and now OSX knows how to honour them.
Again, sorry for the long reply, I hope this helps someone.