Saying "After doing this work we learned..." in a paper I have seen several math papers which end with a statement like "After completing the research in this paper, we learned that all the results are already known."  I wonder what are the situations in which such a comment is appropriate.

Such statements usually sound lazy to me, as though the author opted not to do a literature search at the outset, and after learning of previous work the author opted not to edit the paper (or withdraw it from submission for publication) beyond adding one sentence.  Putting the comment at the end of the paper seems especially questionable, in that the beginning of the paper gives the reader the impression that the results are new.  But I realize that mine is an extreme perspective which does not apply to all situations, and I would be interested to hear other perspectives.
In part, I agree with you: if one finds out the result one has proven is in fact "trivial" (for various definitions of trivial), the "right" thing to do is to not bother submitting it for publication. (Though leaving it up on arXiv, for example, may be useful.) 

But, here are some possible defenses:


In mathematics it is not always the result that matters: the journey getting you there is a big part of it. This sentiment is very well-expressed by Thurston in his essay "On Proof and Progress". Just because one has re-proven a well-known result doesn't necessarily mean that the paper is trivial! If the approach is new this can lead to better understanding. But in this case I, at least, would advertise this fact in the introduction beyond a single sentence. 
As indicated in the comments, most of the time when I saw such a sentence it refers to fairly recent (perhaps even simultaneous) publications. Given that sometimes mathematics research takes time to complete, it is not entirely impossible that the authors genuinely started working on an open problem only to have the result solved in the mean time. And if the problem is somewhat obscure, I think the authors can be forgiven for only doing a detailed literature search before they start their research and not repeatedly doing it once every few months. In this case the language you quoted seems rather appropriate. 
It could honestly be a case of ignorance! Suppose the same essential problem manifests in two distinct field of mathematics which have their own lingo. Then it is quite possible that even most experts in either field would not have recognized easily that the two results are the same. (The "two-way" relation above can also be a "one-way" relation, where researchers in field A generally don't know about result B in field C, while for researchers in field C the applicability of result B to problem D seems obvious.) In this case having the duplicate paper published will (a) help raise awareness that the problem has been solved and (b) perhaps even help the two fields become aware of each others' results. 
Similarly, one may have discovered a simplified proof for a special case of a very powerful general result. While the result may be well-known, having this simplified proof in the special case maybe worthwhile, if only for pedagogy. 
In the modern day of electronic communications this should happen less, but one should remember that in the history of mathematics there are many results which have been discovered, forgotten, and then rediscovered. And even with the advent of MathSciNet, sometimes very technical results are hard to search for: you have to know exactly the right string of keywords to discover it in the literature.