We study the long-time evolution of the trailing shelves that form behind solitary waves moving through an inhomogeneous medium, within the framework of the variable-coefficient Korteweg-de Vries equation. We show that the nonlinear evolution of the shelf leads typically to the generation of an undular bore and an expansion fan, which form apart but start to overlap and nonlinearly interact after a certain time interval. The interaction zone expands with time and asymptotically as time goes to infinity occupies the whole perturbed region. Its oscillatory structure strongly depends on the sign of the inhomogeneity gradient of the variable background medium. We describe the nonlinear evolution of the shelves in terms of exact solutions to the KdV-Whitham equations with natural boundary conditions for the Riemann invariants. These analytic solutions, in particular, describe the generation of small "secondary" solitary waves in the trailing shelves, a process observed earlier in various numerical simulations.