Is there any word/phrase that describes the action of putting the hand on the hair of other people, and moving the hand back and forth?
What you are describing is "petting." Although, petting is usually done to animals. It may be insulting in some cultures to do it to people. Some asian cultures might find it disrespectful as this article explains.
According to Merriam-Webster, definition 1(b) of the verb "pet" is "to stroke in a gentle or loving manner."
If you are messing up their hair then I would go with tousle or ruffle, probably the first, as in "he tousled her hair." If you are just stroking gently you could use stroke: "to pass the hand or an instrument over (something or somebody) lightly or with little pressure; rub gently, as in soothing or caressing." (In response to your other question, this form doesn't only refer to the medical condition) but it seems to me that stroking usually (although perhaps not necessarily) gives the impression of only rubbing in one direction. Since you said back and forth I would possibly also go with either rub, pet or perhaps caress depending on the specific way this action is performed.