Yes. You may enter the U.S. on a visitor visa, marry a U.S. citizen then return home before your visa expires. Where you run into trouble is if you enter on a visitor visa with the intention of marrying and staying in the U.S.
You might have heard about someone who got married in the United States while on a visitor visa, didn't return home, and successfully adjusted their status to permanent resident. Why were these people allowed to stay? Well, it is possible to adjust status from a visitor visa, but people in this scenario were able to prove that they came to the U.S. with honest travel intentions and happened to make a spur-of-the-moment decision to get married.
To successfully adjust status after marrying on a visitor visa, the foreign spouse must show that they had originally intended to return home, and the marriage and desire to stay in the United States was not premeditated. Some couples find it difficult to satisfactorily prove intent but others are successful.