Paper got rejected, should thank original referees when sending it to another journal?
I don't see a gray area here at all.  If someone has helped improve your paper, you must acknowledge their contributions, either by co-authorship, citation, or acknowledgment.  Yes, even in the submission.  Yes, even if there is some small chance that the new referees will notice that your paper was rejected elsewhere and will let that fact unprofessionally bias their review.

Even if your submission is double-blind, you can always thank ███ █████ and ██████ ██ for their helpful comments on an earlier version of the paper.

Less forcefully: It is never a mistake to sincerely express gratitude.


  even if the referee was anonymous, given his tastes and comments, I am almost sure he is someone I already thanked in the paper but of course I can not be 100% certain of it.


You are not supposed to know who the referee is.  So don't guess; just thank the anonymous referee.  There is absolutely no harm in acknowledging the same person twice, once by name and once in their role as anonymous referee.