The first time was inside Belethor's shop, after doing some trades. After tabbing out of the speech menu, I turned around and exited the nearest door. Belethor was still talking to me at the time. When I appeared outside, Belethor appeared beside me and spoke to me some more. (I can't remember if it was random speech, or if he was finishing what he started saying inside). He then just stood near the door of his shop, which was locked.
The second time was when I entered my house in Whiterun. There just happened to be a random NPC walking past at the time, who started saying something as I hit E on the door. When I appeared inside my house, he appeared too and finished what he was saying. He then just stood inside my house.
I would suggest that if an NPC is talking to the player, and the player goes through a door (or anything that sends them to a new area), the NPC's speech should be cancelled and they should stay where they are.