I can't remember the source of this image, but I found a similar image in an Ekulit catalog. One recommended circuit shows just a resistor in series with the piezo buzzer. But this one adds a diode. What's its function?

It's not so much about external sounds but mechanical vibrations. Piezo transducers are capable of quite large voltages. There may be very little current behind them, but that can still punch thru the insulation of a FET gate, for example. The voltage is high enough that a small piezo can actually draw a spark if hit right. This is how the ignitors in many outdoor grill starters work, for example.
Sound vibrations coming thru the air don't cause enough mechanical vibration to generate such high voltages, so that's not really the problem. However, placing the device on a table or dropping it on the floor can produce a short sharp mechanical impulse that can result in high voltage.
However, that's not the reason for the diode. Presumably the IC has protection diodes on it's pin, which should work well enough with the series resistance to clip the voltage. After all, the circuit shown expects this to work in one direction, so it doesn't add up that they think this protection is needed in the other direction. The polarity of external mechanical shocks can't be predicted.
The reason for the diode is that the piezo looks partially inductive to the rest of the circuit. Think what would happen if you replaced the piezo with a inductor in the circuit shown. When the IC output is high, the inductor charges up. Then what happens if the output goes low or high impedance? The inductor current could drive the IC pin voltage negative. So in short, it's a inductive kickback catching diode.
This Microchip discussion claims that the piezo, being a transducer, could potentially create a large voltage if exposed to a loud external sound. The resistor would limit any excessive current from a positive-going excursion, and the diode would clamp any negative-going excursion. Most likely that external diode should be a schottky.
If I may dig up this old question;
Eventhough people and even manufacturers calling it a piezo buzzer, that specific type is probably an electromagnetic buzzer. The diode is to protect against the reactive current in the coil of the buzzer.
Here is the ref design from another example that I encountered recently

The circuit is taken from Murata's catalog Cat.No.P37E-24. It says:
The resistor should be used as shown in Fig. A. A suitable resistance value should be chosen, preferably 1kΩ to 2kΩ. Instead of this measure, a diode may also be applied as shown in Fig. B.
Your figure is Fig. B in the catalog.
In the Murata's FAQ, they say:
Please do not drop the body of the device, subject it to shocks, or change its temperature. That may cause an electric charge (surge voltage) to be generated and destroy the IC or other components. There is a method that uses a Zener diode as in Fig. 1 as an example to protect against that.