Can studying in a good place hurt your application? I have the following situation in mind: consider two people A and B, who are roughly "equally good" in their field and do their bachelors in the same university - a small, not widely known insitution. Then student A stays there for his/her Master's, while student B chooses to do his Master's in a more prestigious place.

At the end, they both apply to a PhD at some competitive place. Coming from a "modest" university, where he easily stands high above the average, student A is highly regarded by his/her professors and so manages to get very good recommendation letters. On the other hand, student B studied in a place where students of his level are more common, and so he doesn't impress his professors as much, and they don't speak so highly about him in recommendation letters.

Do you think that scenarios like this could actually make student A seem a better candidate than student B in practice? In this sense, could studying at a good university hurt your career in the long run? Or would the prestige of the university where student B studied (as well as possibly the prestige of the professors who wrote recommendation letters about him) make up for the more modest recommendation letters?
As others have pointed out, anything can happen.  Of course if you are at the weaker M.S.  school, emphasize your class rank, etc.  But my Bayesian estimate is that you (one) are better off at the STRONGER school.  It will challenge you more, move you more etc.  

Don't try to be a big fish in a small pond. Go out in the ocean and compete with the Great Whites.  Consider Arnold Schwarzenegger.  He didn't stay in the cowtowns of the Steiermark.  He went to Muscle Beach in California.  "New York, New York...if you can make it there, you can make it anywhere."  Be a playah.  

Note:  if there are other reasons, family, weather, economics, pedagogy, etc. that you (one) prefers the easier school, fine.  Of course what matters most is the student, not the school.  If you have the goods, you can prevail anywhere.  But all else equal...go to the harder school.

Just a point of view, but hopefully explaining what is behind the pick.