With the increased number of smart devices in IoT and cloud, communication messages are exchanged extensively throughout the network. This requires secure data transfer between the user and the IoT node, and it entails appropriate mutual authentication and a key establishment mechanism. In this paper, a secure authentication and key agreement mechanism for cloud-based IoT based on elliptic curve cryptography, which provides complete anonymity, has been developed and proposed. The proposed mechanism satisfies multiple security attributes, including confidentiality, the anonymity of the user, anonymity of the IoT node, mutual authentication, secret key establishment, integrity, key privacy, non-repudiation, forward secrecy, and availability. Moreover, security analysis has revealed that the proposed mechanism is safe from replay, user impersonation, gateway impersonation, denial of service, man-in-the-middle, lost/stolen device, de-synchronization, known-key, parallel session, gateway bypassing, and offline password guessing attacks. Formal security analysis of the proposed protocol using BAN logic and ROR model has been carried out to ensure the security of the authentication process and the secrecy of the established key respectively. Comparative analysis of the security functionalities has established that the proposed mechanism provides the highest security in comparison to the other related schemes. The proposed mechanism has also removed the requirement of using a secure communication channel for the registration of an IoT node with the gateway. Though, the proposed mechanism consumes more computational and communication overhead, but in view of trade-off between security functions and performance, the proposed protocol outperforms the other existing IoT authentication protocols.