Actually, it is a bio-molecular mechanism in our brain that recognizes sugar as a sweet substance. When the sugar molecule touches our taste buds, our nervous system sends a signal to our brain that detects the molecule as 'sweet'
There are other substances which we detect as 'sweet' as well and even some animals detect different substances as 'sweet' and some of our 'sweet' substances as something else.