Solved: I was using test.mosquitto mqtt server which does not require formal registration and I did not realise the significance of the client Id until I found this from IBM - "If your MQTT client connects successfully and later disconnects with no apparent reason" - if "Another client is started and connects with the same client ID. ... In this case, WebSphere® MQ accepts the connection from the second MQTT client and forcefully disconnects the first MQTT client." I guess this is related to my problem as when I updated MQTT_CLIENT_ID in user_config.h, the problem went away.
Solved: I was using test.mosquitto mqtt server which does not require formal registration and I did not realise the significance of the client Id until I found this from IBM - "If your MQTT client connects successfully and later disconnects with no apparent reason" - if "Another client is started and connects with the same client ID. ... In this case, WebSphere® MQ accepts the connection from the second MQTT client and forcefully disconnects the first MQTT client." I guess this is related to my problem as when I updated MQTT_CLIENT_ID in user_config.h, the problem went away.