What's the hardware and software detect mechanism of the cellphone Micro USB devices attached and detached?
The simplified legacy OTG works in the following way.

Initial state: The OTG device/host (phone in this case) has ID pin pulled up, this is correct. VBUS is inactive, no voltage supply. ID = HIGH, VBUS = low.

CASE 1: a cable with u-B end is plugged in; the other end is Type-A plug, which goes into USB host (PC for example). In the u-B connector, the ID pin is floating. The phone senses two conditions: ID=HIGH, VBUS=HIGH (normal PC host supplies VBUS). The phone turns its software stack into device mode, will pull D+ up to 3.3V with a 1.5k resistor, and PC host sees the phone either as a mass storage device (old fashion), or MTP - media transfer protocol device.

CASE 2: a cable having ID-pin grounded is plugged in. The phone senses ID=LOW, and VBUS=don't care, normally low as well. This makes the phone to load the USB host software stack, which will turn the VBUS on, and act as a normal USB host. If the other end of a cable is connected to a device, it will pull-up D wire that corresponds to device basic speed.

In theory, the phone receptacle is supposed to be of micro-AB style, and the cable in case2 should have micro-A type shell (rectangular). But this is confusing for customers, and most manufacturers resort to using of u-B receptacle. The OTG swap is done by illegal micro-B plug, with illegally grounded ID pin.

In reality, in CASE1 there is a step of identifying charger signature before the data connection occurs, but this is a different topic.

So, basically, you got it right.