The likely cause is **sugar triggering the release of dissolved gas, followed by coffee stabilizing the bubbles into foam**.

Hot water can hold less dissolved air than cold water. Rapid microwave heating can leave some excess air dissolved in the coffee. Sugar crystals provide **nucleation sites**—tiny surface irregularities where bubbles can start growing—so adding sugar releases many small bubbles at once. [Cambridge’s explanation](https://www3.eng.cam.ac.uk/~hemh1/coffeecup/coffeecup.htm) describes this effect.

The bubbles rise and collect at the surface. Coffee contains compounds that coat bubble surfaces and slow their merging and bursting, which explains the persistent layer. Experiments identify coffee’s polysaccharides as contributors to foam stability. [Coffee foam study](https://pubmed.ncbi.nlm.nih.gov/35426457/)

Your heating-method difference likely reflects how much gas escapes during heating: slower heating, stirring, or boiling can release more of it before you add sugar.

If the coffee is near boiling, sugar can also trigger **steam bubbles from superheated liquid**. However, the foam lasting as the drink cools suggests that its bubbles contain gas such as air; steam condenses during cooling. Avoid overheating the coffee, since adding sugar to superheated liquid can cause sudden vigorous boiling. [Study of microwave superheating](https://digitalcommons.sacredheart.edu/chem_fac/63/)