Genetically identical bacterial cells commonly display different phenotypes. This phenotypic heterogeneity is well known for stress responses, where it is often explained as bet hedging against unpredictable environmental threats. Here, we explore phenotypic heterogeneity in a major stress response of Escherichia coli and find it has a fundamentally different basis. We characterize the response of cells exposed to hydrogen peroxide (H2O2) stress in a microfluidic device under constant growth conditions. A machine-learning model reveals that phenotypic heterogeneity arises from a precise and rapid feedback between each cell and its immediate environment. Moreover, we find that the heterogeneity rests upon cell-cell interaction, whereby cells shield each other from H2O2 via their individual stress responses. Our work shows how phenotypic heterogeneity in bacterial stress responses can emerge from short-range cell-cell interactions and result in a collective phenotype that protects a large proportion of the population.