It's session propagation. You see it when you enter supercruise too.
Providing you're the only player within a session, NPCs are generated when you enter and exit supercruise. Notice how both environments appear empty at first, then ships start to spawn. It's the same in stations. You enter station space - there is nothing (apart from a handful of scripted AI guards), you enter the docking bay and land on the pad and do your stuff at the station. All this time you've remained in the same session and NPCs have propagated. So by the time you're ready to leave, there's a bunch of stuff docking and undocking - clogging up your exit.
Run a test. Dock at a station, do all your station stuff, then log to the main menu. Log back into the game - magically your exit will be hazard free.
The only time this won't happen is if you're not playing solo and enter a session already containing another PC, because their presence has already propagated the session with NPCs - which yours will sync with when you join.