We exploit plausibly exogenous geographical variation in the reduction in domestic demand caused by the Great Recession in Spain to document the existence of a robust, within-firm negative causal relationship between demand-driven changes in domestic sales and export flows. Spanish manufacturing firms whose domestic sales were reduced by more during the crisis observed a larger increase in their export flows, even after controlling for firms’ supply determinants (such as labor costs). This negative relationship between demand-driven changes in domestic sales and changes in export flows illustrates the capacity of export markets to counteract the negative impact of local demand shocks. We rationalize our findings through a standard heterogeneous-firm model of exporting expanded to allow for non-constant marginal costs of production. Using a structurally estimated version of this model, we conclude that the firm-level responses to the slump in domestic demand in Spain could well have accounted for around one-half of the spectacular increase in Spanish goods exports (the so-called ‘Spanish export miracle’) over the period 2009-13.