I think the reason that anxiety and depression causes memory loss is because our brain is given too much to deal with. We worry about so many different things, that our brain is not able to process information and create memories as well as it used to before. So this leads to brain fog and issues with memory.
I think it's important to get treated soon for this reason. My sister has anxiety and she went without treatment for a long time. Now she even forgets memories from our childhood. Her memory is definitely getting worse.