We theoretically investigate the dynamics of a trapped ion immersed in a spatially localized buffer gas. For a homogeneous buffer gas, the ion's energy distribution reaches a stable equilibrium only if the mass of the buffer gas atoms is below a critical value. This limitation can be overcome by using multipole traps in combination with a spatially confined buffer gas. Using a generalized model for elastic collisions of the ion with the buffer-gas atoms, the ion's energy distribution is numerically determined for arbitrary buffer-gas distributions and trap parameters. Three regimes characterized by the respective analytic form of the ion's equilibrium energy distribution are found. Final ion temperatures down to the millikelvin regime can be achieved by adiabatically decreasing the spatial extension of the buffer gas and the effective ion trap depth (forced sympathetic cooling).