It's called microphonics. There are two common causes in flashlights.
But first, you have to understand a bit about how the flashlight works. The modern emitter, the LED itself, is usually an array of multiple LEDs. This makes it brighter, but it's also how to get white light. While each LED requires a voltage of somewhere between about 1.5 and 2.5Volts. When you have multiple LEDs to drive, you can either drive each one individually with its required voltage, or you can strige them in series and drive the chain with the total voltage. The later is easier. But, it requires a voltage perhaps in the 10V range. Batteries typically product 1.5V. You can stack up several for higher voltage, but to get to 10 or 12V, you'd need seven or eight batteries and people don't want a flashlight that big and heavy.
So, there's a power converter circuit in these high-brightness LED flashlights that changes the voltage.
We know that changing a voltage is possible; we see it around us. We see transmission lines on tall towers with "Danger -- High Voltage" signs. And we know that somehow the power in those high-voltage lines ends up in our homes at a voltage that -- while still demanding our care and respect -- is much less dangerous. Somehow that voltage gets changed. The answer, as I suspect that most of us know, is transformers. No, not cartoon robots, but those cylinders we see hanging off of utility poles and those sort of stark-looking big boxes we see through the fences of substations.
But there is a critical difference: batteries produce and LEDs use DC power. Power utilities distribute AC power (in the downtown areas of some cities including Portland, Oregon, they do distribute some DC power used mostly for elevators). Transformers work for AC power, but not DC. So, we can't just connect a transformer between the battery and the LED array.
There are several ways to increase a DC voltage, but they all boil down to some variation of converting it AC. One technique is to convert to AC, use a transformer to increase the voltage, and then convert the higher-voltage AC back to DC. This technique is not the most efficient possible, but it is the easiest to understand.
Anyway, regardless of the exact circuit topology you use, you are going to have a form of AC (at least discontinuous DC) going on.
Sometimes, when you walk near a power distribution transformer, you can hear a low "hum." This is literally the core of the transformer "rattling around" as the AC current changes direction 60 times each second. The sound is actually unintended and a defect, but sometimes making it go away would cost more than its worth.
That "buzz" is at 60Hz (60 oscillations per second) in the US. In some countries, it's at 50Hz, just a bit lower pitch. The physical size and weight of a transformer depends largely on the frequency of the current it must transform. Flashlight designers don't want to use 60Hz because the resulting circuitry would be physically quite large. Instead, they will use 10,000, or 100,000Hz or some such thing, a high frequency.
But the same things happen at higher frequencies. The cores of transformers and inductors physically rattle. Also, the plates of capacitors rattle. This is actually quite common. The ceramic capacitors used at these higher frequencies have very thin plates and they physically rattle and give off a buzzing sound.
Rattling cores in transformers and inductors is annoying. In theory, it's not good for the component, but it's not so bad as to concern the designer in most cases; something else will still fail first. But, those capacitor plates are a problem; those ultra-thin and very brittle ceramic plates can literally crush themselves.
This all makes sense when the switching frequency in the circuit is an audio frequency such as 10,000Hz (human hearing is generally 20-20,000Hz) but to get smaller components, the converter circuits often run much higher, in the 100,000 or even 1,000,000Hz. So, how does that end up being audible? The answer is that if you are tapping your foot to keep time with some music you are enjoying, you can tap with every beat, every other best, every third, every fourth, etc. and still be keeping time with the music.
If the plates in the capacitor or the core of a transformer or inductor can't physically move 1,000,000 times per second, they may move with every 1000th beat and end up buzzing at 1000Hz which is audible.
In general, microphonics in small power converters can be eliminated with minor and inexpensive tweaks to the circuits and/or component selection.
Anyway, the bottom line for me as an electrical engineer is that microphonics in circuits are unacceptable. Leaving them unremediated is sloppy design an can be a precursor of premature failure.