This paper is devoted to the structural stability of a transonic shock passing through a flat nozzle for two-dimensional steady compressible flows with an external force. We first establish the existence and uniqueness of one-dimensional transonic shock solutions to the steady Euler system with an external force by prescribing suitable pressure at the exit of the nozzle when the upstream flow is a uniform supersonic flow. It is shown that the external force helps to stabilize the transonic shock in flat nozzles and the shock position is uniquely determined. Then we are concerned with the structural stability of these transonic shock solutions when the exit pressure is suitably perturbed. One of the new ingredients in our analysis is to use the deformation-curl decomposition to the steady Euler system developed by Weng and Xin [Sci. Sinica Math., 49 (2019), pp. 307–320] to deal with the transonic shock problem.