Why would my back hurt when I do hamstring stretch exercises?

Maybe straining back. You may need to work on stretching for both you hamstrings and lower back. If you are stretching one and the other is still tight, you might strain the other. It is important to do the stretching on a regular basis and I believe it is best to stretch after warming up for awhile.