Starting out from a speaker assembly, we need to generate a voltage that varies at the same rate as we want our membrane to vibrate and thus create sound. This problem also exists in AC powered devices, as the AC comes in at strictly a single frequency, which could generate only a single frequency of sound.
To do this, all digital devices use digital-to-analog (DAC) converters, almost always on a chip. They take some digital input signal at some of their pins, and output a voltage proportional to the value of that digital input on another pin. If we do this fast enough, i.e. feed different numbers in rapid succession, we will get a time-varying output signal.
If we happen to feed it input that corresponds to some audio signal we want to produce, and couple the output pin to a speaker, we are generating sound.
Edit: This is technically not yet alternating current, but AC on top of a DC signal. To get a real alternating current, the output of the converter can be connected through a capacitor (acting as a high-pass filter), which removes the DC part, so only AC can pass through.