As Chenmunka already explains, one may only speculate about Googles working.
But one point is clear, items toward the top of a page are more likely to get a higher score than the same word combination further down.
Another known parameter is the length of a the text block the searched word(s) are in. A longer block results in a higher placement. I guess for both the basic assumption is that main topics for a text are usually already mentioned early on, as well as appearing in real text (in contrast to link text or picture text). In our case it perfectly reflects the 'value' of an answer as well, as upvoted answers stay higher up on the page.
A major point to me seams that comments are usually not only shorter blocks, but as well rather abbreviated, thus not necessary including the search terms in a recognizable way or at all.
Beside that, Stackoverflow/Stackexchange is a major knowledge site, so, much like with Wiki or other major qualifying content providers, it's worth to add a specific scanner to generate higher quality indexing by eliminating 'noise'. SE's delivery generation does a good work of marking all parts with constant labels, so selection of question and answer text is pretty straight forward. In fact, both even use the same class attribute
So I would assume that everything tagged accordingly gets a way higher score, while the rest doesn't get the same bonus. And I wouldn't wonder if the score gets used as well.
Beside that, and as it has been mentioned, it's always good for authors of answers to pick up worthwhile additions found in comments and incooperates them - or encourage commentors to write their own answer if it's about a different viewpoint/solution.