I know, passwd has setuid bit set and hence it can modify root owned /etc/shadow file.
[~]$ ls -l `which passwd`
-rwsr-xr-x. 1 root root 27832 Jan 29 2014 /usr/bin/passwd
But how it is making sure that it can modify only the current account's password if logged in as a non-root user.