Water damage on carpets, even if in a small area, easily causes a musty smell. Carpet fibres can trap moisture, providing an ideal environment for mildew. After major flood damage, you may have little choice, but to replace the carpet and the pad. However, in situations where you have only minor water damage (from spills or leaks, for example) you should be able to save both the carpet and the pad ---especially if you act quickly. If your carpet's musty smell is caused by mildew, the longer you wait, the worse it will usually get.
Open up the room to provide the best ventilation possible. Even if the carpet has long dried, ventilating the room as you work will help.
Pull up the area of carpeting to see if the pad is mildewed. If it is, replace it. You don't have to replace the whole pad --- just the section that has moisture damage.
Vacuum the carpet (if it is dry) with a standard vacuum cleaner. You should get up as much loose soil as you can before cleaning.
Mix half a cup of water to one cup of white vinegar in a clean bucket. Wet a clean sponge in this solution. Test a small area of carpeting for colour fastness. If it seems the solution won't stain the carpet, squeeze excess moisture out and scrub this on the musty area of carpet. Do not dampen the carpet too much --- apply just enough to get the solution into the fibres.
Direct a fan to blow towards the treated area. Then, allow the carpet to dry for at least an hour.
Spray foam carpet shampoo over the area. Rub it in with a sponge and wait until the shampoo has dried; this usually takes at least ten to twenty minutes. When it has dried, vacuum the dry carpet as normal.
If the carpet is still damp, do not vacuum it with a standard vacuum cleaner. You should instead rent or purchase a wet/dry vacuum cleaner to clean it. If, after taking these steps, the musty smell still remains, rent or buy a wet/dry vacuum and shampoo the entire carpet. You can rent steam cleaners and shop vacs from some hardware stores and home improvement stores. For large areas of carpeting, use a sponge mop to apply cleaner, rather than using a sponge.
Musty smell is sometimes a sign of mould, which is a known health hazard. Carpet dyes may bleed or fade when cleaned so don't skip testing an area for colour fastness before cleaning.