Studies outlined in the book "The Health and Welfare of Captive Reptiles" looks in depth at this behavior. It is noted in not only snakes, but lizards. There are several reasons why this happens.
In the studies, the defecation was wholly dependent on the whether soiled objects were left in the enclosure or whether it was completely and thoroughly cleaned. The latency and frequency of defecation was noted and was found to be related to an absence of chemical cues. It occurred equally in new acquisitions and in long term captives.
If small soiled stones or bits of bedding were left in the enclosures, no defecation behavior was noted. In other words, the behavior can be mitigated by leaving familiar scent clues in the cage.
The conclusions pointed at several behavioral culprits:
(1) For a lack of a better word, scent marking.
(2) Increased metabolic activity due to measured increases in cortisol levels due to the combined stresses of unfamiliar scents and handling. Remember, even in habituated animals, measurable increases in cortisol levels were noted after even short handling sessions. Cortisol in humans is a believed aggravator of IBS.
(3) "Emotional Fever". A phenomenon in which an animal has a fever response to handling. This means that some snakes would elevated their baseline temperatures by up to 6 degrees Celsius after handling sessions. The reason is unknown, but the behavior has been noted in snakes, lizards and even mammals.
(4) There is a separate school of thought that deals with how snakes perceive their environment. The theory is that the cues left by cleaning agents rob the animal of it's ability to perceive the enclosure as home. They are unfamiliar and induce elevated stress levels. Researchers were able to curtail this behavior by making sure traces of novel scents were in the cages at all times. Therefore, once the odor was deemed as a natural part of their home, the defecation response to cleaning ceased.
Keep in mind that these same set of studies showed that snakes can tell the difference between their own feces and those of conspecifics.
In short, they look for familiar chemical cues. Whether this is to mark territory or is a process in which familiar scents are a function of their perception of the environment would probably depend on the species and the sex of the animal.
If you don't like the behavior, acclimate the animal to the scent of your cleaner.