The passcode - a four or six digit number - is stored only on the phone (unlike the Apple ID password which is stored on Apple's server) so there is no way of retrieving it once forgotten. Your only option is to erase the phone. Please see
If you forgot the passcode on your iPhone, or your iPhone is disabled - Apple Support