The First Duty began as an idea by Ronald D Moore, a TNG staff writer and producer. Moore developed the idea to script with his friend Naren Shankar who had joined TNG’s staff as a Writer's Guild intern. Shankar and Moore shared an interest in military history, and both were interested in setting an episode at Starfleet Academy, the prestigious training college wherein twenty-fourth century humans learned how to be Starfleet officers.

Moore had been in the US military’s Reserve Officer Training Corps at college and his experience told him that, even in Star Trek’s supposedly idyllic future, such a school would be a high-pressure environment. This prompted him to conceive of a story where Wil Wheaton’s Wesley Crusher, until recently a regular on TNG and still at this point planned as a frequently recurring guest character, would be involved in a scandal that would arise directly out of the pressures on those who aspired to wear a Starfleet uniform. As the story evolved, discussed extensively at a writers’ retreat the TNG staff took in Mexico in late 1991, it became the story of a crime and its cover-up, with Wesley involved in both.

Although Moore and Shankar’s idea won the support of their boss Michael Piller, there was still the opinion of TNG’s other co-showrunner to solicit and Rick Berman was not keen. Piller: "When we pitched it to Rick, he said, ‘It's not Star Trek. Star Trek is about going off into space and exploring new planets. It's not about going back to Earth.’ Piller’s counterargument was that Moore and Shanker’s pitch meant ‘We have the chance to explore an issue.. If you're involved in drugs or teenage misbehavior or crime, and you know that it's the wrong thing and you have the choice of being loyal to your friends or doing what is honest – that is a great issue for us to explore."

By Berman’s account, his initial reluctance was as much about the scale of Wesley’s crime and his involvement in the coverup as the episode being set entirely on Earth. "Wesley is Wesley. He is one of our characters and heroes. He is… not capable of some of the more severe things that were suggested… we basically tempered it down, still keeping it [that] the crime that was serious and would result in a punishment," was what he later recalled the discussion involving. Berman would also later acknowledge that he had developed a habit of arguing on behalf of what he perceived would have been the opinion of Star Trek’s (still then very recently late) creator Gene Roddenberry, even if he did not hold it himself. There is, on the face of it, more than a little of Roddenberry’s own rote criticisms of other people’s ideas in his objections, which amount - as Roddenberry’s often did - to arguing that Star Trek was principally about two things, exploration and human perfectibility.

As the script evolved, Wesley’s crime - committed in concert with three friends - became the covering up of a failed attempt at an illegal flight maneuver, which led to a fourth friend being killed. Berman agreed to the story, on the condition that a maximum of three sets were used. Other aspects of the script gained greater focus. Nick Lorcano, the charismatic leader of Wesley’s squadron, was not initially a large part of the story. His role, and his responsibility for both the accident and for the pressures on other cadets to blame their deceased friend for his own death, grew.

This development had knock-on effects on the episode’s conclusion. While Piller had been keen on Moore and Shankar’s pitch of a story where Wesley was forced to choose between his friends and the truth, he had the opposite view to them about how that binary choice should play out. "I thought he should choose the truth, and Ron thought he couldn't go back on his friends," remembered Piller. The executive producer ultimately pulled rank over the end of his staffer’s story, instructing Moore to have Wesley speak out at the trial. The First Duty would be to the truth.

Moore later elaborated: "In the aired version of events, Wesley steps forward even though the court of inquiry is about to let them all off the hook. In so doing, Wes commits an act of moral courage by standing up for the truth and being punished when to remain silent would've allowed him to go scot-free." In the end, as Moore conceived of it, Wesley and his entire squadron were on the verge of being dismissed from the service at the conclusion of the inquiry. In those circumstances, Wesley coming forward with the truth would not be an altruistic act. It would be selfish. It would be an attempt to save his own career by sacrificing his comrades. The impasse would have been resolved by Lorcano, not Crusher, admitting what had really happened. Piller called this disagreement ‘one of the most rewarding arguments in the history of the production of this show,’ pointing out ‘I think it shows how much we can get into these characters when we find ourselves debating the points they're arguing.’

It's also worth noting that Piller’s version gives the final moment of dramatic self-actualization to one of TNG's lead characters, rather than to a one-off guest role. Although Lorcano would later come close to being more than that. Robert Duncan McNeill, cast as Lorcano in the finished episode, sufficiently impressed Star Trek’s producers that they considered making Lorcano a member of the crew of the USS Voyager. In the end, McNeill joined the show, but Lorcano did not. Although this decision - perceived as in essence McNeil playing the same character with a different name - has been much criticized. There is a counterargument. Paris is, as we learn during the course of Voyager, a fundamentally good person who made a serious error. One who throws up a facade of being a tough guy to stop people from getting close to him. Locarno though is, on the surface, a model officer. He’s smooth and plausible. He takes in Jean Luc Picard. But he’s a golden boy with feet of clay. Locarno is the opposite of Paris, despite being his immediate antecedent.

Also added very late in the day was the meat of the subplot about Picard’s friendship with the Academy’s groundskeeper, Boothby. Boothby had been mentioned (as "the wisest man I ever met") by Picard in the previous season’s Final Mission and Moore was determined to include the character. Unfortunately, Piller felt that while the script did, there wasn’t any reason for that. ‘We had… scenes with Boothby and Picard… we had scenes where they talked… but the scenes weren't clicking and they were the weakest parts of the show.’ It was decided that Boothby and Picard’s shared history should contain a situation analogous to the one Picard faced with Wesley. A moment where the young Picard was on the verge of failing and Boothby helped him in a way that damaged their relationship. The problem was, that no one could work out what it should be.

It was Berman, ultimately, who cracked this story problem. Piller: "Rick said ‘It doesn't matter what Picard's trouble was. We keep that part of our character a mystery’. That was a great decision and what it did for us was put in perspective in a life cycle sort of way that if you make a mistake when you're young and it's found out, you have to pay a price for it. It doesn't mean your life is ruined… you can still become Jean-Luc Picard." It also, in hindsight, allows the story’s two threads to be thematically and emotionally very strongly linked, without seeming contrived.

It was perhaps his late, key contribution as well as the obvious qualities of The First Duty that prompted Berman to relent on his skepticism about its core story, and he described the final episode "as filmmaking and one-hour dramatic television, I thought it was riveting... terrific." He was not alone in that assessment. Later chosen by Entertainment Weekly as one of the ten best episodes of Star Trek: The Next Generation, The First Duty is absolutely first class.