The kind of involuntary, imitative behavior you are describing is usually called behavioral mimicry - "the automatic imitation of gestures, postures, mannerisms, and other motor movements" (Chartrand & Lakin, 2013).
Chartrand and Lakin's (2013) review is a good starting point into the research that has been done on this phenomenon. They provide references to research that has looked at a wide variety of motor movements [references deleted for readability]:
including body posture, face touching, foot shaking, food consumption, pen playing, coloring, handshake, angle and speed, cospeech gestures, and a variety of health-related behaviors (e.g., smoking, taking the stairs rather
than an escalator). Other research has looked at micro movements, such as finger tapping (...) People mimic facial expressions, and emotional reactions of interaction partners (...) In addition, people mimic verbal characteristics of interaction partners, including accents, linguistic style, speech rate, and syntax.
Shrugging is not part of this list, but there is also research that has used shrugging as one example of a behavior that can be imitated (Kurzius & Borkenau, 2015).
Chartrand and Lakin (2013) note that even though people can imitate others intentionally, behavioral mimicry is indeed often automatic:
the mimicry of gross and fine motor movements (e.g.,gestures, mannerisms, finger movements), facial expressions, and vocalizations is often nonconscious, unintentional, and effortless. In fact, people often feel it is uncontrollable and are embarrassed when it is pointed out to them
(Chartrand et al. 2005, White & Argo 2011).
There are several explanations for behavioral mimicry, and some posit that mimicry is (or has been) adaptive (as summarized by in Chartrand & Dalton, 2009).
1. Shared mental representations of perception and behavior
According to this explanation, perceiving a motor movement increases the likelihood of engaging in the same movement, because perception and action have a shared mental representation. According to this argument mimicry is an automatic and passive response, because the regions of the brain that are activated when we perceive action and the ones that are activated when we engage in an action are partly the same (see also mirror neurons, common coding theory).
2. Basic survival benefits of mimicry throughout our evolutionary past
According to this explanation, we have inherited the tendency to imitate others from our prehuman ancestors, because it may have carried important physical benefits for survival. For example, if you notice others of your group running, it may be functional to start running along instead of first scanning the environment for the dangerous predator.
3. Mimicry as social glue
According to this explanation, mimicry fulfills important communicative functions, by communicating togetherness, understanding, and therefore fosters empathy, social bonds and other positive social outcomes. In line with this view is research showing that mimicry increases liking and rapport and that people who have the goal to affiliate with others have a stronger tendency to mimic (e.g., Lakin & Chartrand, 2003).
Chartrand, T. L., & Dalton, A. N. (2009). Mimicry: Its ubiquity, importance, and functionality. In E. Morsella, J. A. Bargh, & P. M. Gollwitzer (Eds.), Oxford handbook of human action, Social cognition and social neuroscience. (pp. 458–483). New York, NY, US: Oxford University Press.
Chartrand, T. L., & Lakin, J. L. (2013). The antecedents and consequences of human behavioral mimicry. Annual Review of Psychology, 64, 285–308. doi:10.1146/annurev-psych-113011-143754
Kurzius, E., & Borkenau, P. (2015). Antecedents and Consequences of Mimicry: A Naturalistic Interaction Approach. European Journal of Personality, 29, 107–124. doi:10.1002/per.1990
Lakin, J. L., & Chartrand, T. L. (2003). Using nonconscious behavioral mimicry to create affiliation and rapport. Psychological Science, 14, 334–339. doi:10.1111/1467-9280.14481