Its complicated. 2 factors promote pyelonephritis. 1) bacteria must invade the kidney. 2) a person's immune system must fail to some degree. Bactereia are common in the urinary tract. But if large amounts of harmful bactereia enter the kidney during lower urinary tract infections or during kidney obstruction (ie. Pregnancy, injury, surgery, congenital disease, or poor bladder emptying), pyelonephritis may develop.
Caught = detection. If you mean "how is it detected?" that is another story. While there are several types of pyelonephritis the most common is an acute form due to bacterial infection. Generally, it is a clinicians suspicion for the infection that leads him or her to make the diagnosis and begin treatment. Clinical symptoms (pain, fever, weakness), abnormal blood and urine tests and ultrasound technology are useful.