The first kings in recorded history would be Sumerian lugals or ensi. Traditionally, the office is considered to have developed from high priests - representatives of the patron deity of their city.
So if this theory is correct, their legitimacy would be theocratic in origin.