Cessation of flow in yield stress fluids results in a stress relaxation process that eventually leads to a finite residual stress. Both the rate of stress relaxation and the magnitude of the residual stresses systematically depend on the preceding flow conditions. To assess the microscopic origin of this memory effect, we combine experiments with large-scale computer simulations, exploring the behavior of jammed suspensions of soft repulsive particles. A spatiotemporal analysis of particle motion reveals that memory formation during flow is primarily governed by the emergence of domains of spatially correlated nonaffine displacements. These domains imprint the configuration of stress imbalances that drive dynamics upon flow cessation, as evidenced by a striking equivalence of the spatial correlation patterns in particle displacements observed during flow and upon flow cessation. Additional contributions to stress relaxation result from the particle packing that reorganizes to minimize the resistance to flow by decreasing the number of locally stiffer configurations. Regaining rigidity upon flow cessation drives further relaxation and effectively sets the magnitude of the residual stress. Our findings highlight that flow in yield stress fluids can be seen as a training process during which the material stores information of the flowing state through the development of domains of correlated particle displacements and the reorganization of particle packings optimized to sustain the flow. This encoded memory can then be retrieved in flow cessation experiments.