Bright X-ray flares are routinely detected by the Swift satellite during the early afterglow of gamma-ray bursts, when the explosion ejecta drives a blast wave into the external medium. We suggest that the flares are produced as the reverse shock propagates into the tail of the ejecta. The ejecta is expected to contain a few dense shells formed at an earlier stage of the explosion. We show an example of how such dense shells form and describe how the reverse shock interacts with them. A new reflected shock is generated in this interaction, which produces a short-lived X-ray flare. The model provides a natural explanation for the main observed features of the X-ray flares --- the fast rise, the steep power-law decline, and the characteristic peak duration Delta t /t= (0.1-0.3).