CBT is part of an integrated approach to these moods, and helps one learn to catch these thoughts as they happen, challenge them, and replace them with more accurate, and more positive assessments.
Negative thoughts breed negative moods
In using CBT we focus in on how thoughts are actually helping to trigger upwellings of depression and anxiety. Your mood is very responsive to thoughts; if you are constantly thinking negative thoughts, then it's impossible that your mood will not reflect it.
CBT has been heavily researched, and has been shown to be very effective in alleviating the worst of depression and anxiety. It can be surprising to see how impactful thoughts are, but it's absolutely true. When you learn to consciously shift your thinking, you both stop triggering the depression/anxiety, but you also start feeling in control of your inner life.
CBT is a way of taking apart old habits of thought, and putting in their place healthier patterns of thought that, because of the way the brain is wired, inevitably lead to more buoyant moods that sustain over time. This is the absolutely essential skills-building part of therapy.
Then, when CBT is undergirded by mindfulness practice (see here), it becomes that much more powerful, because then CBT's inherint emphasis on change is coupled with mindfulness practices' training in letting go and accepting. Strenthening one's ability to regulate you inner experience, and also to accept and allow what can't be immediately changed, is a short way of describing what the cure to these wild moods actually is. CBT is a key part of this cure.