From the Super Mario Wiki
The Vanish Cap is a cap that when worn by Mario in Super Mario 64 , it turns him temporarily into Vanish Mario. In this form Mario can walk through metal fences and certain walls. Mario must first press the blue ! Switch in order to activate the blue ! Blocks which contain vanish caps.