Hello again! When muscles suddenly contract this is called a ‘twitch’ – when a muscle contracts several times in a row in an uncontrolled way. It happens because of a sudden burst of activity in the motor neurons (cells that control movement) in the brain – the motor neuron suddenly fires an electrochemical impulse which a certain group of muscles in the body respond to.
A common twitch is in the leg like you said – or when the eye-lid flutters for a few seconds or even a minute and seems to be ‘pulsing’. People often twitch when they are tired as these sudden bursts of activity in the brain are related to emotional moments like tiredness, joy, sadness or stress.
Muscle reflexes are often just a spasm in the muscle causing it to contract rather than a signal from the brain I think, but there could be different types-one being that the brain subconsciously causes the muscle to move. I sometimes twitch as I am going to sleep and this could be brain-related, when you are switching off to sleep and a thought of something makes you jump a bit.