A washer that smells musty, sour, or mildewy is almost always biofilm — a slime of bacteria, mold, and soap scum that grows inside the tub, on the door-boot gasket (on front-loaders), and in the drain pump and discharge hose.
Three factors create biofilm: cold-water washing, high-efficiency (HE) detergent overuse, and leaving wet laundry in the drum. HE machines use very little water, which sounds efficient but leaves detergent under-rinsed. That detergent film traps organic material from clothes and feeds microbial growth.
The fix is a monthly clean-out cycle and a set of habit changes. Run a tub-clean cycle (present on most modern washers) with either a packet of washer-cleaning tablets (Affresh, Tide Washing Machine Cleaner) or one cup of chlorine bleach. Front-loaders also need the door-boot gasket wiped clean and the detergent-dispenser drawer pulled out and cleaned — both of those are the worst biofilm offenders. Leave the door and dispenser cracked open between loads so the drum dries out.
Long-term habit changes: use the correct amount of HE detergent (less than you think — the dispenser line is a maximum, not a recommendation), run a hot-water load once a week, and don't leave wet laundry sitting in the machine.
If the smell returns within a week of a clean-out cycle, the drain pump or the internal sump often has a sludge buildup that needs to be removed, and the machine also needs a drain-hose inspection for biofilm in the trap.