If the pop-up flash is going off while it is down, it sounds like the flash believe it is up already but is not popping up. The spring that pops the flash up may be disconnected or damaged or the sensor that detects when the flash is closed may be malfunctioning. It might even be both. I would try manually raising the flash while pushing the flash release (not too hard though). I'd also try pushing the flash down to see if it registers as being closed when it is pushed down.
In either case, your best bet is probably to contact your camera manufacturer and send it in to be serviced if it is within the warranty period. If not, unless you are comfortable working on the insides of the camera, it's still probably best to send it out to be repaired. For more opinions refer to Nikon d3100 Manual.