Even in cases where a lightsaber duel starts with a Jedi making the initial strike, the lightsaber duel is frequently only a tiny part of a much larger battle already underway, and those battles are almost never instigated by the Jedi or the Republic. In such cases, the Jedi are only attacking first if you ignore the larger picture. This larger picture typically makes it plain that the bad guys started the fight, and the Jedi intervened after it began, regardless of which lightsaber(s) did what before the other lightsaber(s).
That having been said, there are several reasons why Jedi sometimes attack first:
Because Anakin is involved, and Anakin is a moron
Because a fight is clearly inevitable, and in most cases, has already begun (i.e., a lightsaber duel breaks out in the middle of a larger battle that was already underway)
Because the goal isn't to kill the bad guy, but merely to capture him
Because the goal isn't to kill or capture the bad guy, but for the Jedi to allow the bad guy to kill him (this only happens once, when Obi-Wan fights Vader on the Death Star)
Because Jedi aren't perfect, and don't always live up to their own ideals and principles
Because the writing is sometimes sloppy, inconsistent, and/or self-contradictory
For reasons which aren't clear, and may be the fault of the writers, the characters, or some combination of the two
In some cases, a single example falls under more than one of the above categories (e.g., the first Anakin and Obi-Wan vs. Dooku fight, which definitely fits 1 through 3, and might fit 5, 6, and 7 as well).
Cases of Jedi attacking first:
Attacking because Anakin is involved:
This applies to most duels in which Anakin is involved throughout the prequel trilogy. To put it bluntly, Anakin is a moron, and pretty much everything he does is moronic.
He rushes Dooku and fails; he tries again, fails again, and loses an arm; he tries to stand on a droid assembly line, fails, and gets his arm welded to a conveyor belt; he tries to overcome Obi-Wan's high-ground advantage, fails, and loses both legs and an arm; he becomes Vader, tries to shoot down Luke's X-Wing, fails, and gets flung into space; he tries to catch the Millennium Falcon, fails, and gets shown up by a mere bounty hunter; he tries to capture Leia and Chewbacca, fails, and lets them escape; he tries to convince Luke to join him and rule the galaxy with him, fails, and lets Luke escape; he tries to make Luke turn to the Dark Side, fails, gets an arm lopped off, and ends up killing his boss. Anakin is a walking pile of fail, basically.
When a fight begins with Anakin making the first attack, the vast majority of the blame lies on Anakin's shoulders, and the Jedi are only indirectly at fault because they gave this idiot a lightsaber and training. But I would argue that Anakin is a Jedi in name only, and when he does his stupid-Anakin routine, we're seeing a future Sith at work, not a true Jedi.
Attacking because a fight is clearly inevitable:
This covers many of the instances in which Jedi launch the first attack in a duel, including:
- Obi-Wan vs. Vader
- Obi-Wan and Qui-Gon vs. Darth Maul
- Obi-Wan and Anakin vs. Dooku
- Obi-Wan vs. Anakin (although in this case, Anakin attacked first, as we will see below)
- and others.
In some of these cases, such as Obi-Wan and Qui-Gon vs. Maul, the lightsaber duel itself might start with a Jedi taking a swing, but the lightsaber duel is only a smaller part of a much larger battle already underway.
The Darth Maul fight began after the onset of the assault on the Queen's forces on Naboo, for instance. Likewise, Obi-Wan attacked Anakin only after Anakin helped kill Mace Windu, slaughtered the Younglings, assisted the Clones in their attempt to eradicate the Jedi, and Force-Choked Padme. Furthermore, as the clip below shows, Obi-Wan was the first to draw his lightsaber, but Anakin was the first to use his lightsaber.
In these cases, and several others, the Jedi didn't instigate the fighting, they merely intervened after the larger battle was already well underway. To suggest that Obi-Wan would have been wrong to swing a lightsaber at Anakin before Anakin took a swing at him is misguided, and puts too much focus on the individual (and again, the clip below shows Anakin making the first attack). Anakin was on a rampage, killing people left and right, and clearly intended to continue committing atrocities. Obi-Wan merely stepped in and said 'Pick on someone your own size'. Obi-Wan's motivation wasn't to kill Anakin because Anakin was upsetting him - his motivation was to end the bloodshed.
Bear in mind that at the end of the fight between Anakin and Obi-Wan, Obi-Wan practically begged Anakin to surrender so that Obi-Wan wouldn't have to kill him:
Obi-Wan draws his lightsaber at 1:37, turns it on at 1:42, and Anakin attacks Obi-Wan at 1:44; Obi-Wan disengages, then all but begs Anakin to surrender and avoid being killed/maimed at 7:33
Attacking in the hopes of taking the bad guy alive:
In some cases, Jedi attack first because they want to take the bad guy alive and need to keep him busy until they can subdue him; in such instances, the Jedi would prefer to capture their opponent, not kill him - although they will use lethal force (often without hesitation) when the bad guys leave them no other choice. This is the case when the Jedi attempt to arrest Palpatine, and when Mace Windu holds a lightsaber near Jango Fett's throat, and when Obi-Wan tries to take Dooku alive. So some of the time, an attack isn't an indication of a desire to kill; it is often indicative of a desire to capture the bad guy without killing him.
Before Obi-Wan and Anakin confront Dooku, Obi-Wan makes it clear that the goal is to catch him, not kill him:
Obi-Wan probably still hopes to capture Dooku, but Anakin is stupid and tries to rush Dooku, and fails spectacularly; the fight between Obi-Wan and Dooku is instigated by Dooku, who launches Force Lightning before Obi-Wan attacks
Mace Windu intervenes, but doesn't attack, around 0:10
Mace Windu and three other Jedi attempt to arrest Palpatine, with the goal of having him tried by the Senate; Palpatine strikes first
[to Anakin] I can't take Dooku alone! I need you! If we catch him, we can end this war right now! We have a job to do!
Attacking the bad guy with no intention of capturing or killing him:
Sometimes, the Jedi doesn't hope to kill or capture the bad guy: When Obi Wan attacks Vader in Ep. IV, he does so because he wants Vader to kill him in front of Luke. There is some disagreement over when Obi-Wan decided to let Vader kill him, but I have always believed that Obi-Wan knew he wouldn't leave the Death Star alive, based on the confidence he shows during the duel with Vader (the confidence of a man who is at peace with his imminent demise), and the way he said goodbye to Luke when Obi-Wan left to shut down the tractor beams:
They [the droids and Leia] must be delivered safely or other star systems will suffer the same fate as Alderaan. Your destiny lies along a different path than mine.
- Star Wars, Episode IV: A New Hope
So Obi-Wan attacked first, but had no intention of killing Vader, and he almost certainly planned to let Vader kill him all along.
Vader draws his weapon first, and although Obi-Wan takes the first swing, he isn't striking at Vader himself, he is halfheartedly swatting at Vader's lightsaber. In any case, a fight was clearly inevitable before Obi-Wan swung (Vader wouldn't have drawn his lightsaber if he didn't intend to fight), and Obi-Wan's plan was almost certainly to hold Vader at bay until Luke showed up, then allow Vader to kill him
Attacking because Jedi are imperfect, and sometimes fail to live up to their ideals:
There is also the fact that Jedi are imperfect, just like everyone else. Mace Windu was known for skirting the edge of the Dark Side in lightsaber combat:
Mace Windu was the only Jedi to fully master Vaapad. The other two practitioners of the style, Sora Bulq and Depa Billaba, were unable to master it — instead, it mastered them, driving them insane and leading them to the dark side... A factor that played into Windu's fighting style was his admission that his greatest character flaw was his enjoyment of fighting, and given the nature of the dark side, this was a very dangerous trait for a Jedi to possess. In fact, Windu intentionally created Vaapad to help channel his inner darkness into something more positive.
This is obviously an extreme case, but it highlights the fact that even a trusted and influential Jedi Master like Windu struggled with the Dark Side (in fact, some might argue that Anakin's betrayal of Mace is only slightly more Dark Side than Mace's obvious willingness to execute Palpatine on the spot).
Mace still wants to arrest Palpatine, until he gets hit with Force Lightning and goes half-Dark-Side by trying to kill an unarmed (although far from defenseless) Palpatine; then Anakin goes full-Dark-Side and helps Palpatine kill Mace
Luke Skywalker vs. Vader:
In a similar vein, in the climactic scenes aboard the Death Star in Return of the Jedi, Luke is very much struggling between the Light and Dark Sides of the Force. When he engages Vader in the last round of lightsaber combat, he is catastrophically close to succumbing to the Dark Side. Vader's taunt about turning Leia to the Emperor's service pushes Luke over the edge, and he attacks Vader in a frenzied rage of sledgehammer blows; he is fighting with the power of the Dark Side here. He is only saved from his impending fall to evil after he knocks Vader down, lops off his hand, and is reminded of his own injury from his first encounter with Vader. He throws his lightsaber away, refuses to kill his defenseless opponent, and in so doing, he narrowly escapes a tragic repetition of his father's fall from the Light.
However, the fact that Luke managed to turn away from the Dark Side at the last moment doesn't change the fact that, throughout the entire confrontation with Palpatine and Vader, every time Luke drew his weapon, he did so out of anger, hatred, and passion. These are the paths to the Dark Side, and Luke gave into them more than once.
So, if the question is "Why did Luke attack Vader (and/or Palpatine) first?", the answer is clearly "Because he was struggling to resist the Dark Side, and momentarily faltered. He attacked because he briefly gave in to his hatred and anger."
Luke repeatedly suffers momentary lapses in judgement, and attacks aggressively out of anger, but each time, he manages to rein in his emotions and back down
Rey vs. Kylo:
The same is true of Rey's battle with Kylo Ren in The Force Awakens. Rey beats Kylo in part because she is dipping into some Dark Side powers:
Though Ren was bigger and stronger than Rey, their struggle had nothing to do with physical size. What she lacked in mass, she made up for in ferocity.
For a while she actually drove him backward, until he regained his self-assurance and in turn pressed her. The fight continued to shift back and forth; first he gained the advantage, then an enraged Rey took it back.
...A long moment passed, in which Ren sensed a change in the air, a change in her. Then she opened her eyes and attacked, viciously, in a way she didn’t know she was capable of, striking again and again as Ren was slowly driven back. The flaring energy from the interacting lightsabers was more pronounced than ever in the flurry of her attack. And — Ren went down.
One downward cut, she saw. One quick, final strike, and she could kill him. The landing lights of a shuttle appeared in the distance, coming over the trees in her direction. She had to make a decision, now.
Kill him, a voice inside her head said. It was amorphous, unidentifiable, raw.
Pure vengeful emotion. So easy, she told herself. So quick.
She recoiled from it. From the Dark Side.
- Alan Dean Foster's novelization of The Force Awakens
Rey, more than Luke and Windu, can be forgiven for flirting with the Dark Side, because she has absolutely no training in the Force and doesn't really know what she's doing. As such, it is all the more impressive that she manages to recognize that the voice urging her to kill Kylo is the Dark Side, and therefore, decides to ignore the voice and spare his life.
General problems with consistency and contradictions:
The following isn't strictly related to who attacks first, but might help explain why Jedi (and other characters) sometimes do the things that seem to go against what we've learned about them elsewhere, or otherwise behave inconsistently.
Writer-induced contradictions, inconsistencies, and sloppy retcons:
In other cases, it probably happens because Lucas isn't a very good writer, and constantly contradicts himself and forgets what he wrote in the past.
Keep in mind that this is the guy who invented the Jedi, described them as guardians of peace who only fight defensively, and portrayed them as rejecting emotional attachments, anger, fear, passion, etc, and then decided to name the third movie in the original trilogy Revenge of the Jedi. The movie came so close to being released with that title that there are still countless, original, Revenge of the Jedi movie posters floating around. This is just one of many cases in which the fans seem to understand Lucas' creation better than Lucas himself does. People who grew up watching and loving the original Star Wars trilogy knew (before the prequels came out and muddied the waters) that "Revenge" isn't something that a Jedi would be interested in - indeed, revenge is the kind of thing a Jedi would avoid at all costs, knowing that it reeks of the Dark Side.
As an example of sloppy writing leading to self-contradiction, we have Obi-Wan's line in Ep. III: "Only Sith deal in absolutes". If this were true, Obi-Wan would have to be a Sith, because his statement is itself an absolute. If we apply Obi-Wan's rule about Sith and absolutes rigorously, we find that every Jedi in history is a Sith, because the most common version1 of the Jedi Code is a series of absolute statements:
There is no emotion, there is peace.
There is no ignorance, there is knowledge.
There is no passion, there is serenity.
There is no chaos, there is harmony.
There is no death, there is the Force.
Other examples of characters making surprising factual errors because of writer-induced plot inconsistencies abound: For example, Obi-Wan says Anakin wanted Luke to have his old lightsaber (in fact, Obi-Wan stole the weapon from Anakin's mutilated, charred body; Anakin thought his unborn child was dead; and Anakin never suggested that he wanted to pass the lightsaber down).
These problems are too numerous to describe in full here, but examples include R2's failure to share his knowledge regarding the relationships between Luke, Leia, and Vader; Chewbacca not mentioning that he is old friends with the Jedi Luke seeks out on Dagobah; the absolute lack of any mention of the Sith in the original trilogy; Obi-Wan not recognizing R2 (and vice versa); and so on.
Sometimes it isn't clear whether it is the writers or the characters who are screwing up:
It seems to be almost impossible to explain why Obi-Wan urges Luke to do the one thing that will make him fall to the Dark Side - namely, kill Vader:
You cannot escape your destiny. You must face Darth Vader again.
I can't kill my own father.
Then the Emperor has already won. You were our only hope.
Obi-Wan quite clearly states that Luke has only two options: Kill Vader, or allow the Emperor to win. In the actual event, we find that Obi-Wan has it precisely backwards: if Luke killed Vader, the Emperor would have won; only by sparing Vader's life did Luke avoid the Dark Side and ensure the Emperor's defeat. When Luke was closest to killing Vader, he was also closest to embracing the Dark Side:
You cannot hide forever, Luke.
I will not fight you.
Give yourself to the Dark Side. It is the only way you can save your
friends. Yes, your thoughts betray you. Your feelings for them are strong. Especially for...
Vader stops and senses something. Luke shuts his eyes tightly, in
Sister! So...you have a twin sister. Your feelings have now betrayed
her, too. Obi-Wan was wise to hide her from me. Now his failure is complete. If you will not turn to the Dark Side, then perhaps she will.
Luke ignites his lightsaber and screams in anger, rushing at hisfather
with a frenzy we have not seen before. Sparks fly as Luke and Vader
fight in the cramped area. Luke's hatred forces Vader to retreat out of
the low area and across a bridge overlooking a vast elevator shaft.
Each stroke of Luke's sword drives his father further toward defeat.
The Dark Lord is knocked to his knees, and as he raises his sword to
block another onslaught, Luke slashes Vader's right hand off at the
wrist, causing metal and electronic parts to fly from the mechanical
stump. Vader's sword clatters uselessly away, over the edge of the platform and into the bottomless shaft below. Luke moves over Vader and holds the blade of his sword to the Dark Lord's throat. The Emperor watches with uncontrollable, pleased agitation.
Good! Your hate has made you powerful. Now, fulfill your destiny and
take your father'splace at my side!
Luke looks at his father's mechanical hand, then to his own mechanical,
black-gloved hand, and realizes how much he is becoming like his
father. He makes the decision for which he has spent a lifetime in
preparation. Luke steps back and hurls his lightsaber away.
Never! I'll never turn to the Dark Side. You've failed, Your Highness.
I am a Jedi, like my father before me.
The Emperor's glee turns to rage.
So be it...Jedi.
Luke stands still, as the Emperor reaches the bottom of thestairs. The
Emperor's laughter has turned to anger. He raises his arms toward Luke.
If you will not be turned, you will be destroyed.
Here, the Emperor displays a far better understanding than Obi-Wan does of what would lead Luke to the Dark Side: Palpatine wants Luke to kill Vader, and kill him in hatred. Clearly, Luke's understanding is also superior to Obi-Wan's, because he knew all along that killing Vader would be a mistake:
But why must you confront him?
Because there is good in him. I've felt it. He won't turn me over to the Emperor. I can save him. I can turn him back to the good side. I have to try.
Only when his love for Leia led Luke to strike out at Vader in hatred did his resolve falter. When he was about to do what Obi-Wan wanted him to do, he was teetering on the brink of falling irredeemably to the Dark Side.
How can we explain this? Did Obi-Wan forget how the whole anger-leads-to-the-Dark-Side, we-only-kill-as-a-last-resort parts of the Jedi philosophy? Or did the writers think Obi-Wan was right, and killing Vader was the only way to beat the Emperor? The former seems unlikely - in Episode V, Obi-Wan urged Luke not to face Vader, because the risk of giving into his fear and anger, and thereby falling to the Dark Side, was too great. And when Obi-Wan had the chance to kill Anakin on Mustafar, he didn't take it, presumably because Anakin was defenseless and killing him would have been an act of unnecessary evil. The latter seems unlikely as well, because the writers must have known that in the end, Luke's mercy towards Vader would lead Vader to redeem himself, killing the Emperor, and saving Luke in the process2.
So we can't make sense of why Obi-Wan insisted that Luke had to do exactly what the Emperor wanted him to do, which would lead him to the Dark Side. But we do know for sure that Obi-Wan appears to be absolutely delighted to have been disobeyed and proven wrong by Luke:
The other clear fact we can take away from this situation is simple, and reinforces my earlier point about Jedi fallibility:
Sometimes, even great Jedi Masters are wrong. If a Jedi Master can be as wrong as Obi-Wan was regarding whether Luke should kill Vader, it isn't hard to understand why lesser Jedi, like Anakin (who was more troubled than most with the struggle between Light and Dark) or Luke (who has had only the most rudimentary training in the Force), sometimes behave more aggressively than the Jedi ideal would call for. Even Mace Windu, who was one of the greatest Jedi ever, was constantly skirting the boundary between Light and Dark.
The inverse is also true, of course: sometimes, an incredibly evil devotee of the Dark Side does noble, virtuous things, like Vader did when he sacrificed his own life and killed his master to save Luke.
The same idea carries over to The Force Awakens:
We see a hint of this bad-guy-who-might-not-be-entirely-bad at play in Kylo Ren, who is so torn between Light and Dark that he is mentally unstable, and is emotionally weakened when he kills his father.
The novelization of The Force Awakens makes this clear:
"Stunned by his own action [killing his father, Han], Kylo Ren fell to his knees. Following through on the act ought to have made him stronger, a part of him believed. Instead, he found himself weakened. He did not hear the roar of the enraged Wookiee above, but he did feel the sting of the shot from the bowcaster as it slammed into his side, knocking him back on the walkway."
1 As of a couple of months ago, a new version of the Jedi Code was introduced into the new Disney canon, and although its use seems to be restricted to Younglings, it is less absolute than the official version above. It loses the rigid absolutism of the official code by trading the formula of "There is no _____, there is ______." for the gentler "_______, yet _______.".
Emotion, yet peace.
Ignorance, yet knowledge.
Passion, yet serenity.
Chaos, yet harmony.
Death, yet the Force.
2 We get some insight into Vader's motivation for saving Luke in one of the SW novels:
Vader watched Luke curl into a fetal position as the Emperor hurled an even more staggering wave of lightning at his victim. Vader had no doubt that Luke was about to die. His son screamed.
Not just my son...
The Emperor unleashed another round of lightning.
...or Padmé's son...
Luke screamed louder.
...but my son... who loves me.
Luke's clothes began to smoulder as his body involuntarily spasmed. Suddenly, Vader realised he was no longer concerned about his own personal future. Despite all the terrible, unspeakable things he'd done in his life, he knew he could not stand by and allow the Emperor to kill Luke. And in that moment of awareness, he was Darth Vader no more.
He was Anakin Skywalker.
It took all of his remaining strength to seize the Emperor from behind. The wretched Emperor continued to release lightning bolts, but they veered away from Luke and arced back to crash down upon him and his insurgent apprentice.
- The Rise and Fall of Darth Vader (emphasis mine; italics indicate Vader's thoughts)