From my experience, this is how to deal with a pyro as each.
*NOTE* this is just my opinion, there are probably better methods.
Engineer: You will pretty much be determined on a well placed sentry to get kills as an engineer, so try and be tactical with its placement. If you can locate it at a corner or at a choking point, then you can get it up to annihilate any opposition whilst performing repairs/restocks on the sentry (should have a dispenser close-by). This WILL leave you vulnerable to intelligent demo's, so be careful. The same can be said for soldiers too, so be careful. I can easily get between 25-30 kills using this method before someone finds a way to outsmart or outmanoeuvre me. If you have a frontier justice equipped, then if your sentry gets destroyed, you can devastate the person who took out your sentry

I wouldn't recommend the wrangler for this, because you will spend most of your time repairing the sentry.
Medic: Taking care of a pyro can be awkward using the medic, for the same reason taking care of a heavy or a soldier is awkward as a medic. The syringe gun spreads quite a lot, meaning accuracy is an issue. If you can replace it with the blutsauger (which I find seems to be more accurate) then you can heal yourself whilst firing. Always a plus

If you can flank the pyro, then you will have a higher chance of killing them, but make sure that when you start firing, you dont stop until you need to reload. Speed is everything.
The surgical saw (or any other melee) can make short work of any class if you manoeuvre yourself correctly, but only use it as a desperation method against a pyro! Keeping your distance is integral to staying alive.
Also, as a medic, if you notice a nearby team mate which happens to be a different class then try healing them and allow them to take care of the pyro, a heavy or a soldier can dispose of a pyro much more quickly than you can.
Spy: Generally speaking, avoid pyro's like the plague!! Pyro's are the ultimate defence against spies. It's awkward standing trying to work out a solid strategy, due to how quickly a flamethrower will dispose of you. If you find yourself in a position to backstab them, then great! It feels rather satisfying to backstab a pyro!
Unfortunately, chances are you wont end up in a position to backstab :S
The spy revolver is often under rated, but it's actually pretty damned powerful if you can land a headshot or two! Body shots are exceptionally weak, so try to be as quick and accurate as you can.
Also remember, the spy class was built around the idea of backstabbing and sapping engineer devices, so it's often best to avoid pyro's and hide from them whenever possible. I know that's not great advice but, it's often the best decision.
Thats all the advice I have, and I hope some of it helps. If anyone disagree's with me then, fair enough. Everyone has their own method of playing, so there could be a lot of different methods of taking care of pyro's.
My method: It costs $400,000 to fire this weapon for 12 seconds. It takes me less than 5 to kill any androgynous fire spitters!
