Many (most?) academic papers include a brief acknowledgment section somewhere (the location is not standard). In this section it is frequent to acknowledge help by the referees. One can equally well acknowledge comments by referees of previous versions of the paper. For the sake of clarity it seems best to mention that one is thanking the referee of the previous version. E.g. if I were refereeing the new version, reading
We are grateful for the help provided by the referee.
is ambiguous: does this refer to a previous referee, or am I being thanked in advance for my help? (That's not so good.) Clearer would be something like
This version of the paper takes into account helpful comments provided by the (or "a") referee of a previous version.
If the paper gets accepted in the second journal, one could change to something like
We are grateful for helpful comments from multiple referees on several versions of the paper.
Is it academically necessary? I'm not sure it's ever strictly necessary to acknowledge an anonymous referee -- it seems to me that a situation in which it's vitally necessary to acknowledge someone is a situation in which an anonymous acknowledgment would be insufficient. (Moreover, the way I have always understood it is that referee comments are "being given" to the authors of the paper.) If you feel that a referee's contributions merit coauthorship, (take a deep breath first; this opens a can of worms) you should discuss that with the editor. However, if you got direct intellectual help from the referee and not just helpful remarks -- i.e., if the referee contributed some part of the intellectual content of the paper -- then I would certainly recommend documenting that rather than assuming the credit for yourself.