The ground of the handheld device can weakly couple to earth ground capacitively due to its proximity to the planet. Then bringing the probe of the device (green node) close to a hot line will couple the node slightly to live mains capacitively. This creates a path for a measly current to travel from the power grid through the battery powered circuit and back. This current travels through cascading BJTs to create a very large gain that can be used to detect the presence of a live wire (in this case it just turns on an LED). I’ve never made one myself; I just used one when doing repairs and wondered how it worked. Values of everything in this circuit (capacitance, BJT properties, etc.) are likely not realistic at all so this is more of an explanation of the concept. Without the 1Mohm resistor the circuit behaves like a voltage multiplier so it’s just there to discharge the gate voltage (that’s just a quirk of using a simulator with ideal components). Please don’t make this on a bread board and stick it into your outlet unless you know what you’re doing.