I know about setuid bit on /bin/passwd file which allows us (non-root user) to run that command as root user. running passwd command requires root permissions to change /etc/shadow file.My question is if I am running passwd command as root why I can't change the password of root since I am root while I am running that command?edward@ArchLinux:~$ passwd rootpasswd: You may not view or modify password information for root.Is it passwd command that does not allows non-root user to change the password? Can I write a malicious passwd command with setuid bit that can change any user's password?(Logically I know that this would be disastrous but I want to know how this works in background) 
non-root users can run passwd command as root, why can't they change password of root?
setuid;passwd
passwd(1) checks the credentials of whoever invokes it, and if that isn't root it doesn't allow to change the relevant entries for other users.The scenario you describe is exactly the reason why SUID/SGID programs have to be written with the utmost care, to avoid the possibility of them being tricked into doing something unexpected, or compromised in some way.