This option will retain the intermediate buffers, which are needed to calculate the gradient.
If you leave it as retain_graph=False, the gradient calculated using lossB will still be there, as you didn’t zeroed out the gradients (e.g. by optimizer.zero_grad()). However since the intermediate buffers are freed to save memory, the next backward call will throw an error:

RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.

Setting retain_graph=True allows us to call backward on lossA, which will thereafter free the intermediate buffers, and accumulate the gradients.