The formula you show for dZ^{[1]} is the generic formula that works at any layer. The specific formula you show for dZ^{[2]} corresponds to the case in which layer 2 is the output of a binary classifier with the cross entropy loss function. You can find the derivation of that in this thread.

If you want to see the derivation of the general formula for dZ^{[l]}, that is beyond the scope of this course. Please see this thread for some links that cover the derivation.