Context. In the last years several exoplanets have been discovered that orbit one component of a compact binary system (separation < 50 astronomical units), the probably best-known case is γ-Cephei. So far, all attempts to explain the in-situ formation of these planets has been unsuccessful, in part because of the strong gravitational perturbations of the secondary star on any initial planetesimal swarm.Aims. Here we test whether planetary bodies in compact binaries, in particular γ-Cephei, could have originated from a close encounter with a passing star, assuming initial configurations for the stellar system suitable for planetary formation. In other words, we analyze whether the orbital configuration of the current binary system might have been generated after the formation of the planet, and as a consequence of a close encounter with a third star in hyperbolic orbit. Methods. We performed a series of time-reverse N-body simulations of stellar scattering events in which the present-day configuration of γ-Cephei was used as the initial condition plus a hypothetical third star as an impactor. We analyzed which configurations and system parameters could have given birth to the current system. Results. Depending on the maximum impact velocity allowed for accretional collisions, we find that between ∼ 1% and ∼ 5% of stellar encounters correspond to an "original" system in which planetary formation around the primary star is not inhibited by the secondary, but is acceptable within the classical core-accretion scenario. Thus, although not highly probable, it is plausible that stellar encounters may have played a significant role in shaping these types of exoplanetary systems.