WiFiClient fails to connect to ESP8266 access point
I was able to solve this issue by including the line WiFi.mode(WIFI_STA); before WiFi.begin(ssid, password); and whola! the problem solved.
Although if you go into the code of WiFi.begin(ssid, password); , I think it sets the station mode to STA but not sure why does it need to be set explicitly.
I got this suggestion from another website where people faced the same issue.