This is for a top-loading machine:
Step 1 - With your machine empty, run it full of hot water to the largest load setting. When the tub is full and begins to agitate, add:
3 to 4 cups distilled white vinegar
½ cup baking soda
Allow the machine to agitate for a few moments, then turn off the machine. Let the water sit for 30 minutes - 1 hour.
Step 2 - After allowing the vinegar/baking soda mixture to penetrate the grime in the washing machine tub, restart the machine, allowing it to complete the wash cycle. At the end of the wash cycle, as the tub begins to drain, advance the washing machine to the spin cycle. On many washing machines, this will cause the washing machine to spin as it drains, which can help to further loosen any residue. If yours doesn't do this, simply allow it to drain. Turn off the machine.
Step 3 - Wipe the inside of the washing machine tub with a wet cloth. If desired, use a cloth soaked in a solution of half hot water and half white vinegar. Be sure to wipe all inside surfaces, including the bottom and blades of the agitator and the top of the tub just under the lid. (It is sometimes helpful to use a wire toilet brush around the top rim and in hard-to-reach places.) If the washing machine has a fabric softener and/or a bleach dispenser, remove these and clean thoroughly before replacing.
If, after following these steps the odor remains, the washing machine may have build-up in some of the inner parts. If your washing machine is still under warranty, contact the manufacturer for a repairperson to check it out. If not, contact your local appliance repair shop, and they should be able to clean the machine for you.
How to prevent build-up in your washing machine
To keep your washing machine looking and smelling cleaner for longer, follow these simple steps to reduce build-up:
1. Use less detergent.
What detergent manufacturers don't tell you is that you can often get away with using half the recommended amount (or possibly even less), and your clothes will come out just as clean. The amount you need to use depends on your local water - those with hard water may need to use more detergent, while those with soft water may be able to use extremely small amounts. Experiment to find the optimal detergent amount for your washing machine.
2. Rinse clothing thoroughly after washing.
If you find that your rinse cycle produces a lot of suds, your clothes are not being rinsed thoroughly, and therefore neither is your washing machine. Run an extra rinse cycle, and in the future, try to use less detergent.