In most spicy foods, this is thanks to the same chemicals that cause the burning sensation on your tongue, namely capsaicin or allyl isothiocyanate. Capsaicin is chemical found in fruits of the genus Capsicum, which includes peppers. It is present, usually in relatively high amounts, in the placental tissue that holds the seeds of the peppers, as well as in lower concentrations in other parts of the fruit.
The capsaicin works as a deterrent to stop various animals, particularly mammals that would crunch the seeds, from eating the fruits or otherwise harming the plants or seeds. It also functions as an anti-fungal agent, which further protects the plants.
Allyl isothiocyanate, on the other hand, is a colorless oil that can be found in things like mustard, radishes, and wasabi. Like capsaicin, it serves as a defense for the plant against various animals, as well as works as an anti-fungal agent.
These chemicals end up not only causing a “hot” sensation on your tongue, but also irritate the mucous membranes in your nose, causing them to become inflamed. This triggers those membranes to produce extra amounts of mucous as a defense mechanism to try to keep out whatever unwanted substance or particles are causing the irritation.