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 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 domestic sales and export flows illustrates the capacity of export markets to counteract the negative impact of local demand shocks. We rationalize and interpret our findings by developing a heterogeneous-firm model of exporting featuring non-constant marginal costs of production. Non-constant marginal costs generate firm-level interdependencies in the extensive and intensive margins of sales across domestic and foreign markets. Our quantitative results suggest that the firm-level responses to the slump in domestic demand in Spain explain close to 75% of the observed increase in Spanish exports over the period 2009-13.