Onion does contain sugars when raw, but they are pretty much indigestible and tasteless. Cellulose (vegetable fiber), for example, is a complex carbohydrate which only ruminants can digest with the aid of bacteria in their stomach.
With caramelization, complex sugars in onion split into simpler ones, which are the ones we can taste, by the action of heat. Therefore, fried onion tastes sweeter, and so does tomato, etc.
When sugar cane crops are ready to be harvested, dry leaves are burnt in situ in order to increase the yield of sucrose by the same effect: a fraction of the existing complex sugars are turned into sucrose (saccharose).