Memories are made through a synaptic-strengthening process called long-term potentiation (LTP). Prolonged stress weakens the synapses, which decreases LTP, thus having a negative effect on memory. A mouse study found that when exercise co-occurs with stress, LTP levels are not decreased, but remain normal. The exercising but stressed mice also performed as well on the memory tests as the non-stressed mice, and better than both groups of sedentary mice.
Running is mentioned specifically because the exercise the mice took involved running wheels.
Miller, R. M., Marriott, D., Trotter, J., Hammond, T., Lyman, D., Call, T., Walker, B., Christensen, N., Haynie, D., Badura, Z., Homan, M., & Edwards, J. G. (2018). Running exercise mitigates the negative consequences of chronic stress on dorsal hippocampal long-term potentiation in male mice. Neurobiology of Learning and Memory, 149, 28–38. https://doi.org/10.1016/j.nlm.2018.01.008