Recent discoveries of strongly misaligned transiting exoplanets pose a challenge to the established planet formation theory which assumes planetary systems to form and evolve in isolation. However, the fact that the majority of stars actually do form in star clusters raises the question how isolated forming planetary systems really are. Besides radiative and tidal forces, dense gas aggregates in star-forming regions are potential sources for perturbations to protoplanetary discs or systems. Here we show that subsequent capture of gas from large extended accretion envelopes on to a passing star with a typical circumstellar disc can tilt the disc plane to retrograde orientation, naturally explaining the formation of strongly inclined planetary systems. Furthermore, the inner disc regions may become denser, and thus more prone to speedy coagulation and planet formation. Pre-existing planetary systems are compressed by gas inflows leading to a natural occurrence of close-in misaligned hot Jupiters and short-period eccentric planets. The likelihood of such events mainly depends on the gas content of the cluster and is thus expected to be highest in the youngest star clusters.