Probably as a defence mechanism. There are quite a few creatures with such features, which possibly work because of their resemblance to eyes. In other words, a predator tempted to eat them may "see" some other creature, in effect, staring back at him and that frightens him away.