Onions contain a relatively large amount of sugar. During the cooking process, large sugar molecules are broken down into smaller ones and those have a sweet taste.