Many things. Sometimes they just happen, just like a cold. It depends on your sex and age! if they happen over and over again there is something majotly wrong. If it just happened once its probably not a big deal. You should see a health care provider if you are worried.
Bacteria. Most urinary tract infections (UTI) are secondary to bacterial infection in the bladder. E coli is the most common bacteria causing uti. But don't be fooled...Sometimes people can have symptoms like a UTI (frequency, urgency, pain, etc) and not have an infection. Not all cystitis (bladder irritation) is caused by bacteria.
Bacteria in bladder. Bacteria multiply in bladder consuming urinary nutrients. They enter during sex, thus need to urinate after sex. With poor toilet technique - wiping back to front, or keeing thighs closed with urination thus trapping urine behind labia. Wearing thong or non-cotton underwear. Withholding urination after bladder is full. Using bubble-bath or taking long tub baths. Constipation.