It's just a guess, but in the process of plant evolution, the plant might produce those scents to attract or repel pollinators or as a survival tool, eg. to make themselves unattractive to grazing animals like rabbits and deer. The ones that taste bad to those animals survive and reproduce and the others die out.
They have distinctive essential oils in the leaves that combine to smell like other plants/fruits. There are lemon-y ones and pineapple-y ones and many others, too. Basils have different flavors and scents, as well.
When people discover a plant that smells or tastes appealing, they choose to cultivate it so those plants become more widely available. Then they get cross-bred to produce even more combinations of scents or other attributes. There are probably aromatic herbs that smell awful, too but people wouldn't generally choose to grow those.