Baptism, like Noah’s Ark, portrays Salvation through Judgment (1 Peter 3:18-22).
A comparison is drawn between salvation in the ark and baptism. In both instances, believers are saved through the waters of judgment, since baptism portrays salvation through judgment. The mere mechanical act of baptism does not save, for Peter explicitly says, “not as a removal of dirt from the body,” meaning that the passing of water over the body does not cleanse anyone. Baptism saves you because it represents inward faith, as evidenced by one’s appeal to God for the forgiveness of one’s sins (for a good conscience). Furthermore, baptism “saves” only insofar as it is grounded in the death and resurrection of Jesus Christ. Baptism is a visual representation of the fact that Christians are clothed with Christ (cf. Gal. 3:27), and in union with Christ they share his victory over sin.
(from the ESV Study Bible notes on 1 Peter 3:21)