The Rebel Transports having to fly close to a Star Destroyer in order to escape, I think mainly has to do with the hyperspace lanes. Even in TPM, the Trade Federation didn't have enough ships to cover the entire planet. I'm sure in ESB and TPM, blockading ships were focused near safe hyperspace travel lanes. Granted I know I'm throwing in some EU here, but I feel that's a logical reason as to why the escaping ships fly so close (if you aren't satisfied with it just being a chance in the movie to have a cool action scene). Also I'm sure when an star destroyer detected a transport within its sector, they would try to reposition for an intercept.
The Rebel Transports could have just picked any direction were an star destroyer wasn't positioned, but then they risk the chance of being pursued by fighters or something else, as they make their way to a safe vector to jump to lightspeed.
Heading straight for a safe/known hyperspace route seemed the fastest way to escape, and they have the ion cannon there for this very purpose, to clear a path.