They do this for several reasons. First, it startles the predator. The predator doesn't expect an incredible physical change in a fish it wants to eat and is often scared away. Second, the predator worries that this body-changing fish may actually be able to harm it. Third, pufferfish change their body shape in such a manner that the predators oftentimes can't eat them because they either become too large or too awkward to stick in their mouths.
Some pufferfish can be poisonous.
The poison on these fish have been known to kill several humans in the Indo-Pacific region. So if you ever come in contact with one, be sure to take all precautions. These fish generally spawn in the early summer months. They like to spawn in groups. Large numbers of pufferfish flock to shores in order to find their mates.