They were probably "stacked up" in a holding pattern while waiting for permission to land at a busy airport. This is a common occurrence and the flying maps that pilots use will have a "racetrack" marked up near the destination airport. If things are too busy on the ground to manage incoming traffic, the air traffic controller will instruct the pilot of an arriving plane to divert and "hold as published", which means proceed to the racetrack and go round in circles until you get the all-clear instruction.
Each plane in the holding pattern is assigned their own altitude to circle in, and the ATC will pluck the bottom-most plane out of the stack and clear them to land. Then all the other planes that are stacked up are reassigned the next-lower altitude to hold on and the whole stack "drops" down one level.
This puts the plane that has been holding the longest at the bottom of the stack, and the newest plane to arrive is inserted into the topmost level in the stack.
ADDED IN EDIT:
As pointed out by others here, course changes like the one pictured are often performed by military aircraft, performing maneuvers on training flights. You can pick these out from the ground by noting that such flights do not generally follow the airway patterns for your area. For example, in western Oregon between Eugene and Portland, all the commercial flights high enough to pull contrails follow north-south lines. A jet at cruise that flies overhead here on an east-west track is therefore not a commercial flight because there are no commercial destinations along that line.
BTW once on a flight between Portland and the SFO bay area, our pilot announced that ATC wanted us to arrive 5 minutes or so later than scheduled and in response the pilot made a slow right circle, at cruise. This was approximately over the Oregon-California border. I do not know how often passenger jets get instructed to "hold at cruise" like this.