Any good barista will tell you that if you want to make a nice latte you pour milk into the espresso — not the other way around.

But there’s another style of latte out there, too — the layered latte, or #layeredlatte as you’ll find on Instagram. Created by accident, or by baristas experimenting with new drinks, these striped beverages start with a glass of heated milk and then pour in the espresso. They’re not as pretty or popular as a unicorn Frappuccino or a rainbow latte, but they have their own charm.

Bob Fankhauser, a retired engineer in Portland, Ore., accidentally created his own layered latte at home and wanted to know why these pretty layers form. “It’s a really intriguing phenomenon,” said Mr. Fankhauser. “There’s no obvious reason that the liquid should organize itself into different density layers.”

Last year, Mr. Fankhauser sent an email including photos of his accidental layered lattes to Howard Stone, a chemical engineer who studies fluid dynamics at Princeton University and inspired him and a graduate student to test this out. The team published their results Tuesday in Nature Communications. Anyone can try this at home, but chefs creating layered jellies or bioengineers developing synthetic human tissues may find this one-step process useful, they suggested.

After recreating the latte with their own espresso and milk, the team created a simulated coffee drink, injecting heated, dyed freshwater into heated, denser saltwater to test the scientific parameters that make this spontaneous layering possible. Pouring hot espresso into warm milk at a certain speed, they found, induced an interaction between temperature and density that caused the drink to separate into layers of different densities.

The same basic phenomenon, called double-diffusive convection, creates layers of water in the ocean. There, water containing different amounts of salt has different densities, just like espresso and denser milk in a latte. When the liquids try to mix, layered patterns form as gradients in temperature cause a portion of the liquid to heat up, become lighter and rise, while another, denser portion sinks. This gives rise to convection cells that trap mixtures of similar densities within layers.

Nan Xue, a graduate student in Dr. Stone’s lab who led the study, found that even if you disturb the layers with a gentle stir (or a sip, said Mr. Fankhauser), they will reform and stay put — for minutes, hours, even days.

As long as the mixture is still warmer than the air around it, the stirring creates another density gradient, similar to that produced by pouring. But stir after the latte reaches room temperature — bye-bye stripes.

To create your own layered latte, pour hot espresso over a spoon into a tall glass of milk of about the same temperature. Wait a few minutes for the layers to form as the liquid cools. Success requires experimentation.

In Brooklyn, Casey Lampe, a barista at Stone Fruit Espresso and Kitchen tested variations of this process over five trials. He found that layers form better depending on the speed of your pour, the height of the glass, the ratio of espresso to milk and the temperature and density of the two. There wasn’t time to perfect the experiment before the cafe got busy, but most of the trials produced a few layers, including a thin layer of milk at the bottom of the glass.

Mr. Lampe concluded layered lattes may be better suited for novelty than flavor: “If you stir it and it goes back, it’s almost like at the end, you’re just drinking warm milk,” he said.

But Mr. Fankhauser, who admits he may not catch on to flavor subtleties, thinks the coffee tastes fine and was worth his original curiosity. “To know that you played a little role in somebody discovering something is just delightful,” he said.