diagnosis
Knee problems can be terribly difficult to diagnose. Your seat may be too far forward. Your leg may be pointing outward instead of straight ahead. Your foot may be twisting with each rotation of the pedal. There are a LOT of variables that may affect your knee. I would strongly recommend getting clipless pedals. That alone will elliminate some variables and allow you to carefully set your foot at a place/angle that you can control. I would try to find an experienced bike fitter to ensure your seat is set properly and your foot is set properly...but properly is personal so ultimately you will need to fine tune your situation. I would also encourage you to work at spinning and not mashing. If you aren't spinning at 70-80 you should work toward that goal. It can take a long time for a person to develop the strength to spin and clipless is critical to being able to do that. By spinning in circles you dramatically reduce the strain on the knees.
I have two very bad knees and can't squat without pain in my right knee. When I stand on hills I will return from a ride with sore knees. Ibuprofen has been instrumental in keeping swelling down for me.
You may want to keep a diary. Make one small change at a time and ride a few rides with that change and note the result. Keep in mind that going clipless will be a huge change and it could be good or bad...but if it causes more problems it may not be due to the clipless pedals as much as it is orientation of the foot. You may have to play with that a bit.
Good luck.