The Army Air Force Statistical Digest (World War II) gives figures for USSAF losses by theater by type; for heavy bombers (B-17s and B-24s) in the European Theater of Operations cause of losses is as follows ("Other" excluded):
| Month | Fighters | Flak |
|---|---|---|
| Aug 42 | 0 | 0 |
| Sep 42 | 2 | 0 |
| Oct 42 | 8 | 0 |
| Nov 42 | 10 | 0 |
| Dec 42 | 17 | 0 |
| Jan 43 | 18 | 0 |
| Feb 43 | 21 | 0 |
| Mar 43 | 18 | 0 |
| Apr 43 | 28 | 1 |
| May 43 | 48 | 13 |
| Jun 43 | 78 | 12 |
| Jul 43 | 79 | 29 |
| Aug 43 | 87 | 20 |
| Sep 43 | 46 | 25 |
| Oct 43 | 139 | 38 |
| Nov 43 | 53 | 25 |
| Dec 43 | 85 | 65 |
| Jan 44 | 139 | 27 |
| Feb 44 | 170 | 81 |
| Mar 44 | 178 | 112 |
| Apr 44 | 314 | 105 |
| May 44 | 211 | 122 |
| Jun 44 | 112 | 162 |
| Jul 44 | 80 | 201 |
| Aug 44 | 61 | 238 |
| Sep 44 | 137 | 207 |
| Oct 44 | 36 | 112 |
| Nov 44 | 50 | 146 |
| Dec 44 | 28 | 74 |
| Jan 45 | 49 | 222 |
| Feb 45 | 14 | 157 |
| Mar 45 | 63 | 164 |
| Apr 45 | 72 | 77 |
| May 45 | 1 | 4 |
Up to May 1944 it's fighters that inflict the heaviest losses, from June 1944 it's flak (pedantically both can be attributed to the Luftwaffe, as they generally operated the anti-aircraft guns as well as the fighters).
When the USSAF started operations against Germany it believed the heavy defensive armament of the B-17 and B-24 would be sufficient to protect the bombers with no need for a fighter escort. The "Combat Box" formation meant enemy fighters would be confronted by the guns of multiple bombers when making an attack, and this was a frightening prospect for Luftwaffe pilots such as Franz Stigler: "... eighty-four guns, tracking him in the lead like a spotlight on a stage actor. (...) At five hundred yards, with tracer bullets zipping past his canopy, Franz realized the awful truth of the tail attack. You cannot do this and not be hit." (from A Higher Call).
Luftwaffe tactics evolved: head-on attacks to minimise the time exposed to defensive guns, twin-engined heavy fighters carried increasingly heavy armament including rockets and cannon of up to 50mm, heavily armoured Sturmböcke (battering ram) fighters. Breaking up formations was important, lone bombers were far easier targets for fighters, and this was one of the important roles of flak. Even when fighters were responsible for the majority of bombers actually shot down, flak forced bombers to fly higher and take evasive action, and damaged large numbers of bombers, some of which could then be finished off by fighters.
On missions against Schweinfurt in August and October 1943, around 120 B-17s were shot down, around 60 on each raid. Flak brought down around 20 of them, fighters were responsible for the other 100 (though some of them were damaged by flak first). About 200 bombers returned to base with flak damage, most (but not all) repairable. Clearly unescorted raids were not sustainable. Crippling the Luftwaffe was a key goal of the Combined Bomber Offensive, a prerequisite of launching a cross-channel invasion; the aviation industry was a prime target (the Schweinfurt raids were aimed at ball-bearing factories). It turned out that the only really effective way to destroy enemy aircraft was directly, in combat. As long-range US fighter escorts became available in increasing numbers (the P-38, P-47 and finally and most famously the P-51) the Luftwaffe suffered heavy losses of fighters and fighter pilots. Operation Argument, or "Big Week", in February 1944 saw the heavy bombers of the 8th Air Force accompanied by hundreds of fighters, and though US losses were heavy its forces were growing, whereas Germany was increasingly unable to replace its fighters and pilots. US heavy bomber losses were heaviest of all in April 1944, though in percentage terms they were lower than in October 1943 due to the increasing number of sorties flow (14,464 compared to 2,831). From June, with Allied forces landing in France and their fighters roaming deep into Germany, German pilot training not keeping up with losses and becoming every shorter, and fuel shortages from Allied bombing, German fighters were progressively less of a threat, and heavy flak was the prime cause of US bomber losses.
(See also a previous answer of mine about the effectiveness of anti-aircraft fire.)