The Microchip tools have to read the factory OSCAL RETLW from program memory and preserve it on the host PC through the whole chip erase and reprogramming sequence, then write it back at the end before writing the CONFIG word. This often goes wrong, and stupidly Microchip don't write the OSCCAL value it tried to read to a timestamped log file, nor do they allow user modification of the OSCCAL RETLW, so when it gets FUBARED, there's no easy way back. This was so problematic that all newer PICs with an OSCCAL feature save the factory OSCCAL value in CONFIG memory space so it can be excluded from chip erase.
See
Loss of OSCCAL value during programming.
If you've got a PICkit 2 its easy as its standalone software has an OSCCAL recalibration routine. If you need to manually fiddle with OSCCAL and bandgap bits, I posted a PK2devicefile.dat
[here] that lets the PICkit 2 standalone software (and I believe the PICkit 3 standalone programmer (scripting tool) v3.10) treat the OSCCAL location as normal program memory, and also modify the bandgap calibration bits in the CONFIG word.
There's also an XC8 command line flag to disable OSCCAL handling in the startup code. That lets you do it yourself in main() - check the reset flags to determine the type of the last reset. If it was *NOT* a reset but a program memory wraparound, the OSCCAL RETLW is missing, so skip trying to calibrate and possibly signal an error, otherwise, in inline assembler, call 0x3FF to get the factory OSCCAL in W then stuff it in the OSCCAL SFR.
N.B. the Microchip forums are (as usual) FUBAR and borked.
They ported them to a new forum software and server and in the process managed to break many user contributed internal links. 
Read the old Microchip forum internal post number(5 or 6 numeric digits) from the broken URL and append just the number to:http://www.microchip.com/forums/FindPost/
and you should be able to follow the broken link! 